lookups the ultimate test

JavaScript performance comparison

Revision 2 of this test case created by

Preparation code

<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

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
A
for (var i = 0; i < 100; i++) oa.m();
pending…
B
for (var i = 0; i < 100; i++) ob.m();
pending…
C
for (var i = 0; i < 100; i++) oc.m();
pending…
D
for (var i = 0; i < 100; i++) od.m();
pending…
E
for (var i = 0; i < 100; i++) oe.m();
pending…
F
for (var i = 0; i < 100; i++) of.m();
pending…

Compare results of other browsers

Revisions

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

3 Comments

ponce commented :

It would be cool to be able to run more test to adjust uncertainty.

John-David Dalton commented :

You can adjust the run time by adding this to the "Preparation code" section:

Benchmark.prototype.MAX_TIME_ELAPSED = 15; //seconds (the default is 5)

ponce commented :

Thanks.