prototype vs closures

JavaScript performance comparison

Revision 40 of this test case created by David

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var Person = function (name, lastname, age) {
  	this.name = name;
    this.lastname = lastname;
    this.age = age;
  }
  
  Person.prototype.get_name = function() {
  	return this.name;
  };
  
  Person.prototype.toString = function() {
  	return this.name+' '+this.lastname+' ('+this.age+')';
  };
  
  
  
  var person = function(name, lastname, age) {
  	return {
  		'get_name': function() {
  			return name;
  		},
      'toString': function() {
        return name+' '+lastname+' ('+age+')';
      }
  	};
  };
  
  _p = [];
  _c = [];
  var i;
  for (i = 0; i < 100; ++i) {
    _p.push(new Person('John', 'Doe', 42));
    _c.push(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 read
var i, p = _p, r;
i = p.length;
while (i-- > 0) {
  r = p[i].toString();
}
pending…
Closure read
var i, c = _c;
i = c.length;
while (i-- > 0) {
  r = c[i].toString();
}
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