Classes with Inner Function Calls
JavaScript performance comparison
Preparation code
<script>
var ClassA = function(state) {
this.state = state;
};
ClassA.prototype = {
publicMethod1: function(x) {
return this.state == 'foo' && this.publicMethod2(x);
},
publicMethod2: function(x) {
return this.state == 'foo' && this.publicMethod3(x);
},
publicMethod3: function(x) {
return this.state == 'foo' && this.publicMethod4(x);
},
publicMethod4: function(x) {
return this.state == x;
}
};
var ClassB = function(state) {
function privateMethod1(x) {
return state == 'foo' && privateMethod2(x);
};
function privateMethod2(x) {
return state == 'foo' && privateMethod3(x);
};
function privateMethod3(x) {
return state == 'foo' && privateMethod4(x);
};
function privateMethod4(x) {
return state == x;
};
this.publicMethod1 = privateMethod1;
this.publicMethod2 = privateMethod2;
this.publicMethod3 = privateMethod3;
this.publicMethod4 = privateMethod4;
};
var ClassC = function(state) {
this.state = state;
};
ClassC.prototype = (function() {
function privateMethod1(x) {
return this.state == 'foo' && privateMethod2.call(this, x);
};
function privateMethod2(x) {
return this.state == 'foo' && privateMethod3.call(this, x);
};
function privateMethod3(x) {
return this.state == 'foo' && privateMethod4.call(this, x);
};
function privateMethod4(x) {
return this.state == x;
};
return {
publicMethod1: privateMethod1,
publicMethod2: privateMethod2,
publicMethod3: privateMethod3,
publicMethod4: privateMethod4
};
})();
var ClassD = function(state) {
this.state = state;
};
ClassD.prototype = (function() {
function privateMethod1(self, x) {
return self.state == 'foo' && privateMethod2(self, x);
};
function privateMethod2(self, x) {
return self.state == 'foo' && privateMethod3(self, x);
};
function privateMethod3(self, x) {
return self.state == 'foo' && privateMethod4(self, x);
};
function privateMethod4(self, x) {
return self.state == x;
};
return {
publicMethod1: function(x) {
return privateMethod1(this, x);
},
publicMethod2: function(x) {
return privateMethod2(this, x);
},
publicMethod3: function(x) {
return privateMethod3(this, x);
},
publicMethod4: function(x) {
return privateMethod4(this, x);
}
};
})();
var ClassE = function(state) {
this.state = state;
};
ClassE.prototype = (function() {
var state;
function privateMethod1(x) {
return state == 'foo' && privateMethod2(x);
};
function privateMethod2(x) {
return state == 'foo' && privateMethod3(x);
};
function privateMethod3(x) {
return state == 'foo' && privateMethod4(x);
};
function privateMethod4(x) {
return state == x;
};
return {
publicMethod1: function(x) {
var stack = state;
state = this.state;
var result = privateMethod1(x);
state = stack;
return result;
},
publicMethod2: function(x) {
var stack = state;
state = this.state;
var result = privateMethod2(x);
state = stack;
return result;
},
publicMethod3: function(x) {
var stack = state;
state = this.state;
var result = privateMethod3(x);
state = stack;
return result;
},
publicMethod4: function(x) {
var stack = state;
state = this.state;
var result = privateMethod4(x);
state = stack;
return result;
}
};
})();
var instA = new ClassA('foo');
var instB = new ClassB('foo');
var instC = new ClassC('foo');
var instD = new ClassD('foo');
var instE = new ClassE('foo');
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Plain prototype |
|
pending… |
Closure |
|
pending… |
Prototype + Internal call |
|
pending… |
Prototype + Internal self argument |
|
pending… |
Prototype + Internal self state |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments