Closure Prototype Static Performance

JavaScript performance comparison

Revision 13 of this test case created by Hamidam

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  /* Prototype */
  function A() {
          this.val = 0;
  }
  A.prototype.increment = function(num) {
          this.val += num;
  };
  
  /* Closure */
  function B() {
          this.val = 0;
          this.increment = function(num) {
                  this.val += num;
          };
  }
  
  /** Static method **/
  function C() {
          this.val = 0;
  }
  function increment(c, num) {
          c.val += num;
  }
  
  /** By reference **/
  function _increment(num) {
          this.val += num;
  }
  function D() {
          this.val = 0;
          this.increment = _increment;
  }
  
  /** Object literals **/
  function newE() {
          return {
                  val : 0,
                  increment : function(num) {
                          this.val += num;
                  }
          }
  }
  /** Object literal using new EE(). Comparable to newE() **/
  function EE() {
          return {
                  val : 0,
                  increment : function(num) {
                          this.val += num;
                  }
          }
  }
  
  /** Object with reference **/
  function newF() {
          return {
                  val : 0,
                  increment : _increment
          }
  }
  
  
  /** Object with intended static usage through increment method above **/
  function newG() {
          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();
a.increment(1);
pending…
B: Closure
var b = new B();
b.increment(1);
pending…
C: Static method
var c = new C();
increment(c, 1);
pending…
D: Closure by reference
var d = new D();
d.increment(1);
pending…
E: Object literal
var e = newE();
e.increment(1);
pending…
F: Object literal with reference
var f = newF();
f.increment(1);
pending…
G: Object literal static usage through increment method
var g = newG();
increment(g, 1); 
pending…
H: Object literal without factory method
var h = {val: 0}
increment(h, 1);
pending…
I: Object literal, invoking factory method using new EE()
var ee = new EE();
ee.increment(1);
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