prototype vs closures vs object

JavaScript performance comparison

Revision 103 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function pObject(name) {
      this.x_ = 1;
  }
  
  pObject.prototype.get = function() { return this.x_; };
  pObject.prototype.set = function(_x) { this.x_ = _x; };
  
  function cObject(_x) {
      var x_ = _x;
      return {
          'get': function() { return x_; },
          'set': function(_x) { x_ = _x; }
      };
  };
  
  function oObject(_x) {
      return {
          'x_': _x,
          'get': pObject.prototype.get,
          'set': pObject.prototype.set
      };
  };
  
  var _p = [];
  var _c = [];
  var _o = [];
  for (var i = 0; i < 10000; ++i) {
      _p.push(new pObject(i));
      _c.push(cObject(i));
      _o.push(oObject(i));
  }
  
  var cTotal = 0;
  var pTotal = 0;
  var oTotal = 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
Prototype
for (var i = 0; i < 10000; ++i) {
    var p = _p[i];
    p.x(i);
    pTotal += p.x();
}
pending…
Closure
for (var i = 0; i < 10000; ++i) {
    var c = _c[i];
    c.x(i);
    cTotal += c.x();
}
pending…
Object
for (var i = 0; i < 10000; ++i) {
    var o = _o[i];
    o.x(i);
    oTotal += o.x();
}
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