closure vs prototype vs procedural-esque (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script>
  function A() {
   var a = 0;
   var b = 0;
   var c = 0;
   var d = 0;
  
   this.m1 = function() {
    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() {}
  B.prototype.a = 0;
  B.prototype.b = 0;
  B.prototype.c = 0;
  B.prototype.d = 0;
  B.prototype.m1 = function() {
   this.a += 1;
   this.b += 2;
   this.c += 3;
   this.d += this.a;
  };
  B.prototype.m2 = function() {
   this.a += 4;
   this.b += 5;
   this.c += 6;
   this.d += this.b;
  };
  B.prototype.m3 = function() {
   this.a += 7;
   this.b += 8;
   this.c += 9;
   this.d += this.c;
  };
  B.prototype.m = function() {
   this.m1();
   this.m2();
   this.m3();
  };
  
  C = function() {
   var _m1 = function(o) {
    o.a += 1;
    o.b += 2;
    o.c += 3;
    o.d += o.a;
   },
       
       
       
       _m2 = function(o) {
     o.a += 4;
     o.b += 5;
     o.c += 6;
     o.d += o.b;
       },
       
       
       
       _m3 = function(o) {
     o.a += 7;
     o.b += 8;
     o.c += 9;
     o.d += o.c;
       };
  
   return {
    m: function(o) {
     _m1(o);
     _m2(o);
     _m3(o);
    },
  
    newC: function() {
     return {
      a: 0,
      b: 0,
      c: 0,
      d: 0
     };
    }
   };
  }();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
1. A, m3
for (var i = 0, a; i < 1000; i++) {
 a = new A();
 a.m();
 a.m();
 a.m();
}
ready
2. B, m3
for (var i = 0, b; i < 1000; i++) {
 b = new B();
 b.m();
 b.m();
 b.m();
}
ready
3. A, m10
for (var i = 0, a; i < 1000; i++) {
 a = new A();
 for (var j = 10; j--;) a.m();
}
ready
4. B, m10
for (var i = 0, b; i < 1000; i++) {
 b = new B();
 for (var j = 10; j--;) b.m();
}
ready
5. C, m3
for (var i = 0, b; i < 1000; i++) {
 c = C.newC();
 C.m(c);
 C.m(c);
 C.m(c);
}
ready
6. C, m10
for (var i = 0, b; i < 1000; i++) {
 c = C.newC();
 for (var j = 10; j--;) C.m(c);
}
ready

Revisions

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