instance apply vs prototype reference vs super

JavaScript performance comparison

Revision 3 of this test case created by tomalec

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function Parent() {
    this.a = 1;
  }
  Parent.prototype.method = function() {
    return this.a;
  }
  
  
  function Child_apply() {
    this.a = 1;
    this.b = 2;
  }
  Child_apply.prototype = new Parent();
  Child_apply.prototype.parent = Parent.prototype;
  child_apply = new Child_apply();
  Child_apply.prototype.method = function(arg) {
    return Parent.prototype.method.apply(this, arguments) + arg;
  }
  
  function Child_protref() {
    this.a = 1;
    this.b = 2;
  }
  Child_protref.prototype = new Parent();
  Child_protref.prototype.parent = Parent.prototype;
  child_protref = new Child_protref();
  Child_protref.prototype._method = Parent.prototype.method;
  Child_protref.prototype.method = function(arg) {
    return this._method() + arg;
  }
  
  function Child_super() {
    this.a = 1;
    this.b = 2;
  }
  Child_super.prototype = new Parent();
  Child_super.prototype.parent = Parent.prototype;
  child_super = new Child_super();
  Child_super.prototype.method = function(arg) {
    return this.parent.method.apply(this, arguments) + arg;
  }
  
  
  function Child_call() {
    this.a = 1;
    this.b = 2;
  }
  Child_call.prototype = new Parent();
  Child_call.prototype.parent = Parent.prototype;
  child_call = new Child_call();
  Child_call.prototype.method = function(arg) {
    return Parent.prototype.method.call(this, arg) + arg;
  }
  
  
  function Child_superCall() {
    this.a = 1;
    this.b = 2;
  }
  Child_superCall.prototype = new Parent();
  Child_superCall.prototype.parent = Parent.prototype;
  child_superCall = new Child_superCall();
  Child_superCall.prototype.method = function(arg) {
    return this.parent.method.call(this, arg) + arg;
  }

};
</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
Apply
child_apply.method(1);
pending…
Prototype ref
child_protref.method(1);
pending…
Super
child_super.method(1);
pending…
Call
child_call.method(1);
pending…
Call super
child_superCall.method(1);
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