call vs apply

JavaScript performance comparison

Revision 9 of this test case created by tenbits

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function Obj() {
      var that = "that";
    }
    Obj.prototype.target = function(test) {
      return test;
    };
   
    function call(fn, cntx, arg){
       return fn.call(cntx, arg);
    }
   
    function callMany(fn, cntx /* ... */){
        switch(arguments.length){
           case 3:
             return fn.call(cntx, arguments[2]);
           case 4:
             return fn.call(cntx, arguments[2],arguments[3]);
           case 5:
             return fn.call(cntx, arguments[2],arguments[3],arguments[4]);
           case 6:
             return fn.call(cntx,
                     arguments[2],
                     arguments[3],
                     arguments[4],
                     arguments[5]);
        }
    }
   
    function apply(fn, cntx, arg){
        return fn.apply(cntx, [arg]);
    }
   
};
</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
apply(Obj.prototype.target, {}, "YES");
pending…
call
call(Obj.prototype.target, {}, "YES");
pending…
callMany
callMany(Obj.prototype.target, {}, "YES");
pending…
call - direct
Obj.prototype.target.call({}, "YES");
pending…
apply - direct
Obj.prototype.target.apply({}, ["YES"]);
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