prototype vs closures

JavaScript performance comparison

Revision 109 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function ClassicObject(x, y) {
    this.x = x;
    this.y = y;
  }
  
  ClassicObject.prototype.getSum = function() {
    return this.getX() + this.getY();
  };
  ClassicObject.prototype.setSum = function(v) {
    this.setX(v - this.getY());
  };
  
  ClassicObject.prototype.getX = function() { return this.x; };
  ClassicObject.prototype.getY = function() { return this.y; };
  ClassicObject.prototype.setX = function(v) { this.x = v; };
  ClassicObject.prototype.setY = function(v) { this.y = v; };
  
  
  function ClosureObject(x, y) {
    function getX() { return x; }
    function getY() { return y; }
    function setX(v) { x = v; }
    function setY(v) { y = v; }
    return {
      getSum: function() {
        return getX() + getY();
      },
      setSum: function(v) {
        setX(v - getY());
      }
    };
  }
  
  var classic_object = new ClassicObject(10, 20);
  var closure_object = ClosureObject(10, 20);

};
</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
Prototype init & 10 reads
var p = new ClassicObject(10, 20);

for (var i = 0; i < 10; ++i) {
    p.getSum();
}
pending…
Closure init & 10 reads
var c = ClosureObject(10, 20);

for (var i = 0; i < 10; ++i) {
    c.getSum();
}
pending…
Prototype init & 10 writes
var p = new ClassicObject(10, 20);

for (var i = 0; i < 10; ++i) {
    p.setSum(40);
}
pending…
Closure init & 10 writes
var c = ClosureObject(10, 20);

for (var i = 0; i < 10; ++i) {
    c.setSum(40);
}
pending…
Prototype init & 20 reads
var p = new ClassicObject(10, 20);

for (var i = 0; i < 20; ++i) {
    p.getSum();
}
pending…
Closure init & 20 reads
var c = ClosureObject(10, 20);

for (var i = 0; i < 20; ++i) {
    c.getSum();
}
pending…
Prototype init & 20 writes
var p = new ClassicObject(10, 20);

for (var i = 0; i < 20; ++i) {
    p.setSum(40);
}
pending…
Closure init & 20 writes
var c = ClosureObject(10, 20);

for (var i = 0; i < 20; ++i) {
    c.setSum(40);
}
pending…
Prototype init & 30 reads
var p = new ClassicObject(10, 20);

for (var i = 0; i < 30; ++i) {
    p.getSum();
}
pending…
Closure init & 30 reads
var c = ClosureObject(10, 20);

for (var i = 0; i < 30; ++i) {
    c.getSum();
}
pending…
Prototype init & 30 writes
var p = new ClassicObject(10, 20);

for (var i = 0; i < 30; ++i) {
    p.setSum(40);
}
pending…
Closure init & 30 writes
var c = ClosureObject(10, 20);

for (var i = 0; i < 30; ++i) {
    c.setSum(40);
}
pending…
Prototype init & 100 reads
var p = new ClassicObject(10, 20);

for (var i = 0; i < 100; ++i) {
    p.getSum();
}
pending…
Closure init & 100 reads
var c = ClosureObject(10, 20);

for (var i = 0; i < 100; ++i) {
    c.getSum();
}
pending…
Prototype init & 100 writes
var p = new ClassicObject(10, 20);

for (var i = 0; i < 100; ++i) {
    p.setSum(40);
}
pending…
Closure init & 100 writes
var c = ClosureObject(10, 20);

for (var i = 0; i < 100; ++i) {
    c.setSum(40);
}
pending…
Prototype init
var p = new ClassicObject(10, 20);
pending…
Closure init
var c = ClosureObject(10, 20);
pending…
Prototype read and write
classic_object.getSum();
classic_object.setSum(40);
classic_object.getSum();
classic_object.setSum(30);
pending…
Closure read and write
closure_object.getSum();
closure_object.setSum(40);
closure_object.getSum();
closure_object.setSum(30);
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