prototype vs closures: read and write

JavaScript performance comparison

Revision 67 of this test case created by eight

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.set_name1 = function(name) {
  	this.name = name;
  };
  
  Person.prototype.set_name2 = function(name) {
  	this.name = name;
  };
  
  Person.prototype.set_name3 = function(name) {
  	this.name = name;
  };
  
  Person.prototype.set_name4 = function(name) {
  	this.name = name;
  };
  
  Person.prototype.set_name5 = function(name) {
  	this.name = name;
  };
  
  var person = function(pname) {
  	return {
  		'get_name': function() {
  			return pname;
  		},
  		'set_name': function(name) {
  			pname = name;
  		},
  		'set_name1': function(name) {
  			pname = name;
  		},
  		'set_name2': function(name) {
  			pname = name;
  		},
  		'set_name3': function(name) {
  			pname = name;
  		},
  		'set_name4': function(name) {
  			pname = name;
  		},
  		'set_name5': function(name) {
  			pname = name;
  		}
  	};
  };
  
  // closure using bind
  var makePerson = function(){
  	var get_name = function(){
  		return this.name;
  	};
  	
  	var set_name = function(name){
  		this.name = name;
  	};
  	
  	var set_name1 = function(name){
  		this.name = name;
  	};
  	
  	var set_name2 = function(name){
  		this.name = name;
  	};
  	
  	var set_name3 = function(name){
  		this.name = name;
  	};
  	
  	var set_name4 = function(name){
  		this.name = name;
  	};
  	
  	var set_name5 = function(name){
  		this.name = name;
  	};
  	
  	var init = function(name){
  		var person = {
  			name: name
  		};
  		
  		return {
  			get_name: get_name.bind(person),
  			set_name: set_name.bind(person)
  		};
  	}
  
    return init;
  }();
  
  var _p = new Person('John');
  var _c = person('John');
  var _b = makePerson('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 init & 10 writes
var c = _c;

for (var i = 0; i < 10; ++i) {
    c.set_name('Jane');
}
pending…
Closure init with bind & 10 reads
var b = _b;

for (var i = 0; i < 10; ++i) {
    b.get_name();
}
pending…
Closure init with bind & 10 writes
var b = _b;

for (var i = 0; i < 10; ++i) {
    b.set_name('Jane');
}
pending…
Prototype init & 10 reads
var p = _p;

for (var i = 0; i < 10; ++i) {
    p.get_name();
}
pending…
Closure init & 10 reads
var c = _c;

for (var i = 0; i < 10; ++i) {
    c.get_name();
}
pending…
Prototype init & 10 writes
var p = _p;

for (var i = 0; i < 10; ++i) {
    p.set_name('Jane');
}
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