call vs apply (v9)

Revision 9 of this benchmark created by tenbits on


Setup

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]);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
apply
apply(Obj.prototype.target, {}, "YES");
ready
call
call(Obj.prototype.target, {}, "YES");
ready
callMany
callMany(Obj.prototype.target, {}, "YES");
ready
call - direct
Obj.prototype.target.call({}, "YES");
ready
apply - direct
Obj.prototype.target.apply({}, ["YES"]);
ready

Revisions

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