prototype vs closures (v96)

Revision 96 of this benchmark created by Maxi S. on


Setup

function Person1(name) {
  	this.name = name;
  }
  
  Person1.prototype.get_name = function() {
  	return this.name;
  };
  
  Person1.prototype.set_name = function(name) {
  	this.name = name;
  };
  
  
  var createPerson2 = function(name) {
  	var p = {
  		name: name
  	};
  
  	return {
  		get_name: function () {
  			return p.name;
  		},
  		set_name: function (name) {
  			p.name = name;
  		}
  	};
  };
  
  
  function get_name(p) {
  	return p.name;
  }
  
  function set_name(p, name) {
  	p.name = name;
  }
  
  var createPerson3 = function (name) {
  	var p = {
  		name: name
  	};
  	return {
  		get_name: get_name.bind(null, p),
  		set_name: set_name.bind(null, p),
  	}
  };
  
  var i;
  var p1List = [];
  var p2List = [];
  var p3List = [];
  
  for (i = 0; i < 10000; i += 1) {
   p1List.push(new Person1('John #' + i));
   p2List.push(createPerson2('John2 #' + i));
   p3List.push(createPerson3('John3 #' + i));
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype init
var p1 = new Person1('John');
ready
Closure init
var p2 = createPerson2('John');
ready
Prototype read
p1.get_name();
ready
Closure read
p2.get_name();
ready
Prototype write
p1.set_name('Jane');
ready
Closure write
p2.set_name('Jane');
ready
Closure 2 init
var p3 = createPerson3('John');
ready
Closure 2 read
p3.get_name();
ready
Closure 2 write
p3.set_name('Jane');
ready
Prototype multiple read
for (i = 0; i < p1List.length; i += 1) {
  p1List[i].get_name();
}
ready
Prototype multiple write
for (i = 0; i < p1List.length; i += 1) {
  p1List[i].set_name('Jane #' + i);
}
ready
Closure multiple read
for (i = 0; i < p2List.length; i += 1) {
  p2List[i].get_name();
}
ready
Closure multiple write
for (i = 0; i < p2List.length; i += 1) {
  p2List[i].set_name('Jane #' + i);
}
ready

Revisions

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