prototype vs closures (v102)

Revision 102 of this benchmark created on


Setup

function pPerson(name) {
      this.name = name;
  }
  
  pPerson.prototype.get_name = function() { return this.name; };
  pPerson.prototype.set_name = function(name) { this.name = name; };
  
  function cPerson(name) {
      var name_ = name;
      return {
          'get_name': function() { return name_; },
          'set_name': function(name) { name_ = name; }
      };
  };
  
  function oPerson(name) {
      return {
          'name': name,
          'get_name': pPerson.prototype.get_name,
          'set_name': pPerson.prototype.set_name
      };
  };
  
  var _p = [];
  var _c = [];
  var _o = [];
  for (var i = 0; i < 10000; ++i) {
      _p.push(new pPerson('John'));
      _c.push(cPerson('John'));
      _o.push(oPerson('John'));
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype read
for (var i = 0; i < 10000; ++i) {
    _p[i].get_name();
}
ready
Closure read
for (var i = 0; i < 10000; ++i) {
    _c[i].get_name();
}
ready
Object read
for (var i = 0; i < 10000; ++i) {
    _o[i].get_name();
}
ready

Revisions

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