Call versus Apply

JavaScript performance comparison

Test case created

Preparation code

<script>

</script>
<script>
Benchmark.prototype.setup = function() {
    function MyClass()
    {
    this.sum = sum;
    return this;
   
    function sum(a,b)
    {
    return a+b;
    }
   
    }
   
    var myObj = new MyClass();
   
    function test1(a,b,c,d,e,f,g,h)
    {
    myObj.sum(a,b,c,d,e,f,g,h);
    }
   
    function test2(a,b,c,d,e,f,g,h)
    {
    myObj.sum(a,b);
    }
   
    function test3(a,b,c,d,e,f,g,h)
    {
    myObj.sum.call(myObj, a,b,c,d,e,f,g,h);
    }
   
    function test4(a,b,c,d,e,f,g,h)
    {
    myObj.sum.apply(myObj, [a,b,c,d,e,f,g,h]);
    }
   
    function test5()
    {
    myObj.sum.apply(myObj, arguments);
    }
};
</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
Direct call
test1(1,2);
pending…
Direct call (less args)
test2(1,2);
pending…
Call
test3(1,2);
pending…
Apply
test4(1,2);
pending…
Apply:arguments
test5(1,2);
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