prototype vs closures

JavaScript performance comparison

Revision 86 of this test case created by tav

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function Person(name, location) {
  	this.name = name;
    this.location = name + "lives at: " + location;
  }
  
  Person.prototype.getinfo = function() {
  	return this.name + "/" + this.location;
  };
  
  Person.prototype.setinfo = function(name, location) {
  	this.name = name;
    this.location = name + "lives at: " + location;
  };
  
  var person = function(name, location) {
  	return {
  		'getinfo': function() {
      	return this.name + "/" + this.location;
  		},
  		'setinfo': function(n, l) {
      	name = n;
        location = n + "lives at: " + l;
  		}
  	};
  };
  
  var operson = function(name, location) {
    return {name: name, location: location};
  };
  
  var getInfo = function(p) {
  	return p.name + "/" + p.location;
  }
  
  var setInfo = function(p, name, location) {
  	p.name = name;
    p.location = name + "lives at: " + location;
  }
  
  var _p = new Person('John', 'London');
  var _c = person('John', 'London');
  var _o = operson('John', 'London');

};
</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', 'London');
pending…
Closure init
var c = person('John', 'London');
pending…
Prototype read
_p.getinfo();
pending…
Closure read
_c.getinfo();
pending…
Prototype write
_p.setinfo('Jane', 'Paris');
pending…
Closure write
_c.setinfo('Jane', 'Paris');
pending…
Obj init
var o = operson('John', 'London');
pending…
Obj read
getInfo(_o);
pending…
Obj write
setInfo(_o, 'Jane', 'Paris');
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