prototype vs closures

JavaScript performance comparison

Revision 76 of this test case created by Anonymous

Preparation code

<script type="application/javascript;version=1.8">
"use strict";
function Person(name) {
	this.name = name;
  const name2 = name
  this.get_name_const = function() {
    return name2
  }

}

Person.prototype.get_name_property = function() {
	return this.name;
};

var _c = [];
var i;
for (i = 0; i < 10000; ++i) {
  _c.push(new Person('John'));
}
var escape_hatch = null
</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;
for (i = 0; i < _c.length; ++i) {
  escape_hatch = _c[i].get_name_property();
}
pending…
Closure read
var i;
for (i = 0; i < _c.length; ++i) {
  escape_hatch = _c[i].get_name_const();
}
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