lookups the ultimate test (v2)

Revision 2 of this benchmark created on


Description

We test the speed of member access with various ways of creating objects.

Preparation HTML

<script>
  function A() {
  
   var a = 0;
   var b = 0;
   var c = 0;
   var d = 0;
  
   this.m1 = function() {
    // avoid bug in Chrome 10.x that will optimize away the `a` reference
    if (typeof a == 'number') {
     a += 1;
     b += 2;
     c += 3;
     d += a;
    }
   };
  
   this.m2 = function() {
    a += 4;
    b += 5;
    c += 6;
    d += b;
   };
  
   this.m3 = function() {
    a += 7;
    b += 8;
    c += 9;
    d += c;
   };
  
   this.m = function() {
    this.m1();
    this.m2();
    this.m3();
   };
  }
  
  function B() {
  
   this.a = 0;
   this.b = 0;
   this.c = 0;
   this.d = 0;
  
   this.m1 = function() {
    this.a += 1;
    this.b += 2;
    this.c += 3;
    this.d += this.a;
   };
  
   this.m2 = function() {
    this.a += 4;
    this.b += 5;
    this.c += 6;
    this.d += this.b;
   };
  
   this.m3 = function() {
    this.a += 7;
    this.b += 8;
    this.c += 9;
    this.d += this.c;
   };
  
   this.m = function() {
    this.m1();
    this.m2();
    this.m3();
   };
  }
  
  // assigning the whole prototype
  
  function C() {}
  C.prototype = {
   a: 0,
   b: 0,
   c: 0,
   d: 0,
   m1: function() {
    this.a += 1;
    this.b += 2;
    this.c += 3;
    this.d += this.a;
   },
   m2: function() {
    this.a += 4;
    this.b += 5;
    this.c += 6;
    this.d += this.b;
   },
   m3: function() {
    this.a += 7;
    this.b += 8;
    this.c += 9;
    this.d += this.c;
   },
   m: function() {
    this.m1();
    this.m2();
    this.m3();
   }
  };
  
  // assigning the prototype
  
  function D() {}
  D.prototype.a = 0;
  D.prototype.b = 0;
  D.prototype.c = 0;
  D.prototype.d = 0;
  
  D.prototype.m1 = function() {
   this.a += 1;
   this.b += 2;
   this.c += 3;
   this.d += this.a;
  };
  D.prototype.m2 = function() {
   this.a += 4;
   this.b += 5;
   this.c += 6;
   this.d += this.b;
  };
  D.prototype.m3 = function() {
   this.a += 7;
   this.b += 8;
   this.c += 9;
   this.d += this.c;
  };
  D.prototype.m = function() {
   this.m1();
   this.m2();
   this.m3();
  };
  
  // assigning the whole prototype, but members in constructors
  
  function E() {
   this.a = 0;
   this.b = 0;
   this.c = 0;
   this.d = 0;
  
  }
  E.prototype = {
  
   m1: function() {
    this.a += 1;
    this.b += 2;
    this.c += 3;
    this.d += this.a;
   },
   m2: function() {
    this.a += 4;
    this.b += 5;
    this.c += 6;
    this.d += this.b;
   },
   m3: function() {
    this.a += 7;
    this.b += 8;
    this.c += 9;
    this.d += this.c;
   },
   m: function() {
    this.m1();
    this.m2();
    this.m3();
   }
  };
  
  // object literal
  
  function F() {
   return {
    a: 0,
    b: 0,
    c: 0,
    d: 0,
    m1: function() {
     this.a += 1;
     this.b += 2;
     this.c += 3;
     this.d += this.a;
    },
    m2: function() {
     this.a += 4;
     this.b += 5;
     this.c += 6;
     this.d += this.b;
    },
    m3: function() {
     this.a += 7;
     this.b += 8;
     this.c += 9;
     this.d += this.c;
    },
    m: function() {
     this.m1();
     this.m2();
     this.m3();
    }
   };
  }
  
  
  
  var oa = new A();
  var ob = new B();
  var oc = new C();
  var od = new D();
  var oe = new E();
  var of = new F();
  
  // increase default run time of benchmarks
  Benchmark.prototype.MAX_TIME_ELAPSED = 15;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
A
for (var i = 0; i < 100; i++) oa.m();
ready
B
for (var i = 0; i < 100; i++) ob.m();
ready
C
for (var i = 0; i < 100; i++) oc.m();
ready
D
for (var i = 0; i < 100; i++) od.m();
ready
E
for (var i = 0; i < 100; i++) oe.m();
ready
F
for (var i = 0; i < 100; i++) of.m();
ready

Revisions

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