Super Calls
JavaScript performance comparison
Preparation code
<script>
var Parent = function(z) {
this.x = 1;
this.y = z;
};
Parent.prototype.method = function(n) {
this.y += n + 1;
};
function empty() {};
var object = function(inherits) {
empty.prototype = inherits.prototype;
return new empty();
};
// Closure Call Indirect
var base = Parent.prototype;
var ClassA = function(z) {
Parent.call(this, z);
};
ClassA.prototype = object(Parent);
ClassA.prototype.method = function(n) {
base.method.call(this, n + 1);
};
// Closure Call Direct
var baseMethod = Parent.prototype.method;
var ClassB = function(z) {
Parent.call(this, z);
};
ClassB.prototype = object(Parent);
ClassB.prototype.method = function(n) {
baseMethod.call(this, n + 1);
};
// Prototype Invoke
var ClassC = function(z) {
this.ParentConstructor(z);
};
ClassC.prototype = object(Parent);
ClassC.prototype.ParentConstructor = Parent;
ClassC.prototype.ParentMethod = Parent.prototype.method;
ClassC.prototype.method = function(n) {
this.ParentMethod(n + 1);
};
// Prototype Call
var ClassD = function(z) {
this.baseConstructor(z);
};
ClassD.prototype = object(Parent);
ClassD.prototype.baseConstructor = Parent;
ClassD.prototype.base = Parent.prototype;
ClassD.prototype.method = function(n) {
this.base.method.call(this, n + 1);
};
var a = new ClassA(2);
var b = new ClassB(2);
var c = new ClassC(2);
var d = new ClassD(2);
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
Prototype Call |
|
pending… |
Prototype Invoke |
|
pending… |
Closure Call Direct |
|
pending… |
Closure Call Indirect |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments