instance apply vs prototype reference vs super

JavaScript performance comparison

Revision 2 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() {
      return Parent.prototype.method.apply(this, arguments) + 1;
    }
   
    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() {
      return this._method + 1;
    }
   
    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() {
      return this.parent.method.apply(this, arguments) + 1;
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Apply
child_apply.method();
pending…
Prototype ref
child_protref.method();
pending…
Super
child_super.method();
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. Here’s a list of current revisions for this page:

0 comments

Add a comment