Prototype vs. this (v41)

Revision 41 of this benchmark created by kuus on


Setup

function T() {
    }
    
    T.prototype.myDefault1 = null;
    T.prototype.myDefault2 = [1, 2, 3];
    T.prototype.myDefault3 = {something: 'else'};
    
    function Tt() {
      this.myDefault1 = null;
      this.myDefault2 = [1, 2, 3];
      this.myDefault3 = {something: 'else'};
    }
    
    function L() {
    }
    
    L.prototype = {
      myDefault1: null,
      myDefault2: [1, 2, 3],
      myDefault3: {something: 'else'}
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Using prototype
for (var i = 1; i <= 20000; i++) {
  var x = new T();
  x.myDefault1;
  x.myDefault2;
  x.myDefault3;
}
ready
Using this
for (var i = 1; i <= 20000; i++) {
  var x = new Tt();
  x.myDefault1;
  x.myDefault2;
  x.myDefault3;
}
ready
Using prototype (literal notation)
for (var i = 1; i <= 20000; i++) {
  var x = new L();
  x.myDefault1;
  x.myDefault2;
  x.myDefault3;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.