prototype vs closures (v40)

Revision 40 of this benchmark created by David on


Setup

var Person = function (name, lastname, age) {
  	this.name = name;
    this.lastname = lastname;
    this.age = age;
  }
  
  Person.prototype.get_name = function() {
  	return this.name;
  };
  
  Person.prototype.toString = function() {
  	return this.name+' '+this.lastname+' ('+this.age+')';
  };
  
  
  
  var person = function(name, lastname, age) {
  	return {
  		'get_name': function() {
  			return name;
  		},
      'toString': function() {
        return name+' '+lastname+' ('+age+')';
      }
  	};
  };
  
  _p = [];
  _c = [];
  var i;
  for (i = 0; i < 100; ++i) {
    _p.push(new Person('John', 'Doe', 42));
    _c.push(person('John'));
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype read
var i, p = _p, r;
i = p.length;
while (i-- > 0) {
  r = p[i].toString();
}
ready
Closure read
var i, c = _c;
i = c.length;
while (i-- > 0) {
  r = c[i].toString();
}
ready

Revisions

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