prototype vs closures (v21)

Revision 21 of this benchmark created on


Setup

function Person(name) {
  	this.name = name;
  }
  
  Person.prototype.get_name = function() {
  	return this.name;
  };
  
  Person.prototype.set_name = function(name) {
  	this.name = name;
  };
  
  Person.prototype.do_nothing = function() {
  };
  
  // let's say there are 10 more methods
  Person.prototype.f0 = function() { return 'f0a'; };
  Person.prototype.f1 = function() { return 'f1a'; };
  Person.prototype.f2 = function() { return 'f2a'; };
  Person.prototype.f3 = function() { return 'f3a'; };
  Person.prototype.f4 = function() { return 'f4a'; };
  Person.prototype.f5 = function() { return 'f5a'; };
  Person.prototype.f6 = function() { return 'f6a'; };
  Person.prototype.f7 = function() { return 'f7a'; };
  Person.prototype.f8 = function() { return 'f8a'; };
  Person.prototype.f9 = function() { return 'f9a'; };
  
  var person = function(name) {
  	var p = {
  		name: name
  	};
  
  	return {
  		'get_name': function() {
  			return p.name;
  		},
  		'set_name': function(name) {
  			p.name = name;
  		},
      'do_nothing': function() {
      },
      'f0': function() { return 'f0b' },
      'f1': function() { return 'f1b' },
      'f2': function() { return 'f2b' },
      'f3': function() { return 'f3b' },
      'f4': function() { return 'f4b' },
      'f5': function() { return 'f5b' },
      'f6': function() { return 'f6b' },
      'f7': function() { return 'f7b' },
      'f8': function() { return 'f8b' },
      'f9': function() { return 'f9b' }
  	};
  };
  
  var _p = new Person('John');
  var _c = person('John');

Test runner

Ready to run.

Testing in
TestOps/sec
Prototype init
var p = new Person('John');
ready
Closure init
var c = person('John');
ready
Prototype read
_p.get_name();
ready
Closure read
_c.get_name();
ready
Prototype write
_p.set_name('Jane');
ready
Closure write
_c.set_name('Jane');
ready
Prototype invoke function
_p.do_nothing();
ready
Closure invoke function
_c.do_nothing();
ready

Revisions

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