prototype vs closures (v38)

Revision 38 of this benchmark created by David Bonnet on


Setup

var Person = function (name) {
  	this.name = name;
  }
  
  Person.prototype.get_name = function() {
  	return this.name;
  };
  
  Person.prototype.set_name = function(name) {
  	this.name = name;
  };
  
  var person = function(name) {
  	var p = {
  		name: name
  	};
  
  	return {
  		'get_name': function() {
  			return p.name;
  		},
  		'set_name': function(name) {
  			p.name = name;
  		}
  	};
  };
  
  _p = [];
  _c = [];
  var i;
  for (i = 0; i < 10000; ++i) {
    _p.push(new Person('John'));
    _c.push(person('John'));
  }

Test runner

Ready to run.

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

Revisions

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