Test name | Executions per second |
---|---|
Using this | 755735.7 Ops/sec |
Using prototype | 471129.1 Ops/sec |
Using Factory | 1067671.2 Ops/sec |
function T(i) {
this.counter = i;
}
T.prototype.myMethod = function() {
if (this.counter == 20000) {
console.log('Hey T!');
}
};
function Tt(i) {
this.counter = i;
this.myMethod = function() {
if (this.counter == 20000) {
console.log('Hey Tt!');
}
};
}
function factoryPattern(i){
var counter = i;
function myMethod() {
if (counter == 20000) {
console.log('Hey Factory!');
}
}
return {
myMethod: myMethod
}
}
for (var i = 1; i <= 100; i++) {
var x = new T(i);
x.myMethod();
}
for (var i = 1; i <= 100; i++) {
var x = new Tt(i);
x.myMethod();
}
for (var i = 1; i <= 100; i++) {
var x = factoryPattern(i);
x.myMethod();
}