closures vs prototype v.1

Benchmark created by zit 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();
 };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
A 0m
for (var i = 0, a; i < 100; i++) a = new A();
ready
B 0m
for (var i = 0, b; i < 100; i++) b = new B();
ready
A 3m
for (var i = 0, a; i < 100; i++) {
 a = new A();
 a.m();
 a.m();
 a.m();
}
ready
B 3m
for (var i = 0, b; i < 100; i++) {
 b = new B();
 b.m();
 b.m();
 b.m();
}
ready
A 10m
for (var i = 0, a; i < 100; i++) {
 a = new A();
 for (var j = 10; j--;) a.m();
}
ready
B 10m
for (var i = 0, b; i < 100; i++) {
 b = new B();
 for (var j = 10; j--;) b.m();
}
ready
A 100m
for (var i = 0, a; i < 100; i++) {
 a = new A();
 for (var j = 100; j--;) a.m();
}
ready
B 100m
for (var i = 0, b; i < 100; i++) {
 b = new B();
 for (var j = 100; j--;) b.m();
}
ready

Revisions

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