Closure Prototype Static Performance

JavaScript performance comparison

Revision 14 of this test case created by Bergi

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function _increment(num) {
          this.val += num;
  }
  
  /* Prototype */
  function A() {
          this.val = 0;
  }
  A.prototype.increment = _increment;
  
  /* Closure with property */
  function B() {
          this.val = 0;
          this.increment = function(num) {
                  this.val += num;
          };
  }
  /* Closure with variable */
  function C() {
          var val = 0;
          this.increment = function(num) {
                  val += num;
          };
          this.getVal = function() {
                   return val;
          };
  }
  
  /* Constructor and static function */
  function D() {
          this.val = 0;
  }
  function increment(o, num) {
          o.val += num;
  }
  
  /* Constructor with reference */
  function E() {
          this.val = 0;
          this.increment = _increment;
  }
  
  /* factory with property closure */
  function newB() {
          return {
                  val : 0,
                  increment : function(num) {
                          this.val += num;
                  }
          }
  }
  
  /* factory with closure and variable */
  function newC() {
          var val = 0;
          return {
                  increment : function(num) {
                          val += num;
                  },
                  getVal : function() {
                          return val;
                  }
          }
  }
  
  /** factory with reference **/
  function newE() {
          return {
                  val : 0,
                  increment : _increment
          }
  }
  
  
  /** factory with static function **/
  function newD() {
          return {
                  val : 0
          }
  }
  

};
</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: Prototype
var a = new A();
for (var i=100; i--;)
    a.increment(1);
a.val;
pending…
B: Closure (property) constructor
var b = new B();
for (var i=100; i--;)
    b.increment(1);
b.val;
pending…
B: Closure (property) factory
var b = newB();
for (var i=100; i--;)
    b.increment(1);
b.val;
pending…
C: Closure (variable) constructor
var c = new C();
for (var i=100; i--;)
    c.increment(1);
c.getVal();
pending…
C: Closure (variable) factory
var c = newC();
for (var i=100; i--;)
    c.increment(1);
c.getVal();

pending…
D: static with constructor
var d = new D();
for (var i=100; i--;)
    increment(d, 1);
d.val;
pending…
D: static with factory
var d = newD();
for (var i=100; i--;)
    increment(d, 1);
d.val;
pending…
D: static with literal
var d = {val: 0};
for (var i=100; i--;)
    increment(d, 1);
d.val;
pending…
E: Reference in constructor
var e = new E();
for (var i=100; i--;)
    e.increment(1);
e.val;
pending…
E: Reference in factory
var e = newE();
for (var i=100; i--;)
    e.increment(1);
e.val;
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.

0 Comments