jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Can you gain a performance boost in the prototype pattern by adding a reference to functions on the prototype?
Three "Classes"... Customer1: Prototype Customer2: Prototype with reference Customer3: Closure
In Customer2, I add a variable in the constructor: this.greet = this.constructor.prototype.greet; The idea is to increase performance by adding a direct reference to the prototype's function meaning it shouldn't have to traverse the prototype chain as far.
The tests are split between setting up the objects and calling functions on pre-created objects. In the tests P is Prototype, PR is Prototype with Reference, and Cl is Closure.
function Customer1(name) {
this.name = name;
}
Customer1.prototype = {
constructor: Customer1,
greet: function () {
return this.name + ' says hi!';
}
};
function Customer2(name) {
this.name = name;
this.greet = this.constructor.prototype.greet;
}
Customer2.prototype = {
constructor: Customer2,
greet: function () {
return this.name + ' says hi!';
}
};
function Customer3(name) {
var _name = name;
this.greet = function() {
return _name + ' says hi!';
};
}
var c4 = new Customer1();
var c5 = new Customer2();
var c6 = new Customer3();
Ready to run.
Test | Ops/sec | |
---|---|---|
P Setup |
| ready |
PR Setup |
| ready |
Cl Setup |
| ready |
P Func |
| ready |
PR Func |
| ready |
Cl Func |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.