apply vs call vs invoke (v18)

Revision 18 of this benchmark created on


Setup

var obj = {};
    var args = ['a','b','c','d'];
    var a = 4;
    function fn(a,b,c,d) {return a+b+c+d;}

Test runner

Ready to run.

Testing in
TestOps/sec
apply
fn.apply(obj, args);
ready
call
fn.call(obj, args[0], args[1], args[2], args[3]);
ready
invoke
obj.fn = fn;
obj.fn(args[0], args[1], args[2], args[3]);
delete obj.fn;
ready
apply (undef)
fn.apply(undefined, args);
ready
call (undef)
fn.call(undefined,args[0], args[1], args[2], args[3]);
ready
invoke (undef)
switch (a) {
        case  0:  fn();
        case  1: fn(args[0]);
        case  2: fn(args[0], args[1]);
        case  3: fn(args[0], args[1], args[2]);
        case  4: fn(args[0], args[1], args[2], args[3]);
        default: fn.apply(self, args);
      }
ready

Revisions

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