prototype vs closures

JavaScript performance comparison

Revision 65 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.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;
  		}
  	};
  };
  
  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
prototypes
for (var i = 0; i < 2000; ++i) {
    function Person() {}
    Person.prototype.name01 = function() {};
    Person.prototype.name02 = function() {};
    Person.prototype.name03 = function() {};
    Person.prototype.name04 = function() {};
    Person.prototype.name05 = function() {};
    Person.prototype.name06 = function() {};
    Person.prototype.name07 = function() {};
    Person.prototype.name08 = function() {};
    Person.prototype.name09 = function() {};
    Person.prototype.name10 = function() {};
    Person.prototype.name11 = function() {};
    Person.prototype.name12 = function() {};
    Person.prototype.name13 = function() {};
    Person.prototype.name14 = function() {};
    Person.prototype.name15 = function() {};
    Person.prototype.name16 = function() {};
    Person.prototype.name17 = function() {};
    Person.prototype.name18 = function() {};
    Person.prototype.name19 = function() {};
    Person.prototype.name20 = function() {};
}
pending…
Prototype init & 10 reads
var p = new Person('John');

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

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

for (var i = 0; i < 10; ++i) {
    p.set_name('Jane');
}
pending…
Closure init & 10 writes
var c = person('John');

for (var i = 0; i < 10; ++i) {
    c.set_name('Jane');
}
pending…
functions
for (var i = 0; i < 2000; ++i) {
    function Person() {
        this.name01 = function() {};
        this.name02 = function() {};
        this.name03 = function() {};
        this.name04 = function() {};
        this.name05 = function() {};
        this.name06 = function() {};
        this.name07 = function() {};
        this.name08 = function() {};
        this.name09 = function() {};
        this.name10 = function() {};
        this.name11 = function() {};
        this.name12 = function() {};
        this.name13 = function() {};
        this.name14 = function() {};
        this.name15 = function() {};
        this.name16 = function() {};
        this.name17 = function() {};
        this.name18 = function() {};
        this.name19 = function() {};
        this.name20 = function() {};
    }
}
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