prototype vs closures

JavaScript performance comparison

Revision 95 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function Person1(name) {
  	this.name = name;
  }
  
  Person1.prototype.get_name = function() {
  	return this.name;
  };
  
  Person1.prototype.set_name = function(name) {
  	this.name = name;
  };
  
  
  var Person2 = 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 Person3 = function (name) {
  	var p = {
  		name: name
  	};
  	return {
  		get_name: get_name.bind(null, p),
  		set_name: set_name.bind(null, p),
  	}
  };
  
  var p1 = new Person1('John');
  var p2 = Person2('John');
  var p3 = Person3('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
Closure write
p2.set_name('Jane');
pending…
Closure 2 init
var p3 = Person3('John');
pending…
Closure 2 read
p3.get_name();
pending…
Closure 2 write
p3.set_name('Jane');
pending…
Closure read
p2.get_name();
pending…
Prototype write
p1.set_name('Jane');
pending…
Prototype read
p1.get_name();
pending…
Closure init
var p2 = Person2('John');
pending…
Prototype init
var p1 = new Person1('John');
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