prototype vs closures vs object (v104)

Revision 104 of this benchmark created on


Setup

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;

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype
for (var i = 0; i < 10000; ++i) {
    var p = _p[i];
    p.set(i);
    pTotal += p.get();
}
ready
Closure
for (var i = 0; i < 10000; ++i) {
    var c = _c[i];
    c.set(i);
    cTotal += c.get();
}
ready
Object
for (var i = 0; i < 10000; ++i) {
    var o = _o[i];
    o.set(i);
    oTotal += o.get();
}
ready

Revisions

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