prototype vs closures

JavaScript performance comparison

Revision 21 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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');

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Prototype init
var p = new Person('John');
pending…
Closure init
var c = person('John');
pending…
Prototype read
_p.get_name();
pending…
Closure read
_c.get_name();
pending…
Prototype write
_p.set_name('Jane');
pending…
Closure write
_c.set_name('Jane');
pending…
Prototype invoke function
_p.do_nothing();
pending…
Closure invoke function
_c.do_nothing();
pending…

Compare results of other browsers

Revisions

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

0 Comments