apply vs call vs invoke (v4)

Revision 4 of this benchmark created by Igor Minar on


Setup

var fn = function(a,b,c,d) {return a+b+c+d;},
        obj = {},
        fnBound = fn.bind(obj),
        fnJsBound = jsBind(obj, fn),
        fnStupidBind = rewrite(obj, fn);
    
    function jsBind(self, fn) {
      return function() {
        return arguments.length
           ? fn.apply(self, arguments)
           : fn.call(self);
       };
    }
    
    function rewrite(self, fn) {
      var f = eval('(' + fn.toString().replace(/this/g, '__this__') + ')');
      return function() {
        __this__ = self;
        var value = f();
        __this__ = null;
        return value;
      }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
apply
fn.apply(obj, ['a','b','c','d']);
ready
call
fn.call(obj, 'a', 'b', 'c', 'd');
ready
invoke
obj.fn = fn;
obj.fn('a','b','c','d');
delete obj.fn;
ready
apply (undef)
fn.apply(undefined, ['a','b','c','d']);
ready
call (undef)
fn.call(undefined, 'a', 'b', 'c', 'd');
ready
invoke (undef)
fn('a','b','c','d');
 
ready
bind
var fn1 = fn.bind(obj);
fn1('a','b','c','d');
ready
bind (prebound)
fnBound('a','b','c','d');
ready
jsBind (prebound)
fnJsBound('a','b','c','d');
ready
fnStupidBind
fnStupidBind('a','b','c','d');
ready

Revisions

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