function .call vs .apply

JavaScript performance comparison

Test case created by tomalec

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    "use strict";
    var Parent = function(){};
    Parent.prototype.functionHeadSimple = function() {
      return 5;
    }
   
    var global = 5;
    Parent.prototype.functionHeadAdvanced = function(a, b, c, d, e, f, g, h, i, j, k, l, m) {
      if (a !== 50) {
        return 0;
      }
      return global + a + b + c + d + e + f + g + h + i + j + k + l + m + a + b + c + d + e + f + g + h + i + j + k + l + m;
    }
   
    Parent.prototype.functionHeadAdvancedAdvanced = function(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) {
      if (a !== 50) {
        return 0;
      }
      return global + a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p + q + r + s + t + u + v + w + x + y + z;
    }
    var Child = function(){};
    Child.prototype = new Parent();
   
   
    Child.prototype.callSimple = function() {
      return Parent.prototype.functionHeadSimple.call(this);
    }
   
   
    Child.prototype.callAdvanced = function(a, b, c, d, e, f, g, h, i, j, k, l, m) {
      return Parent.prototype.functionHeadAdvanced.call(this, a, b, c, d, e, f, g, h, i, j, k, l, m);
    }
   
    Child.prototype.callAdvancedAdvanced = function(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) {
      return Parent.prototype.functionHeadAdvanced.call(this, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z);
    }
   
    Child.prototype.applySimple = function() {
      return Parent.prototype.functionHeadSimple.apply(this, arguments);
    }
   
    Child.prototype.applyAdvanced = function() {
      return Parent.prototype.functionHeadAdvanced.apply(this, arguments);
    }
   
    Child.prototype.applyAdvancedAdvanced = function() {
      return Parent.prototype.functionHeadAdvancedAdvanced.apply(this, arguments);
    }
   
    child = new Child();
};
</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
.call without any arguments
child.callSimple();
pending…
.call with many arguments
child.callAdvanced(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
pending…
.call even more arguments
child.callAdvancedAdvanced(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
pending…
.apply without any arguments
child.applySimple();
pending…
.apply with many arguments
child.applyAdvanced(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
pending…
.apply with even more arguments
child.applyAdvancedAdvanced(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment