Bind vs call vs native

Benchmark created by Justin on


Description

How much overhead does a bind closure add?

Setup

var push = [].push,
        slice = [].slice;
    
    function bind(fn, thisArg) {
      var curried, reset;
      if (arguments.length > 2) {
        curried = slice.call(arguments, 2);
        reset = curried.length;
        return function() {
          curried.length = reset; // reset arg length
          return fn.apply(thisArg, arguments.length
            ? (push.apply(curried, arguments), curried)
            : curried
          );
        };
      }
      return function() {
       return arguments.length
         ? fn.apply(thisArg, arguments)
         : fn.call(thisArg);
      };
    };
    
    function test() {
    
    }
    
    var context = {};
    var bound = bind(test, context);
    var partial = bind(test, context, 1);
    
    if (test.bind) {
      var nativeBound = test.bind(context);
      var nativePartial = test.bind(context, 1);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
native empty
nativeBound();
ready
simple empty
bound();
ready
native args
nativeBound(1);
nativeBound(1, 2, 3);
ready
simple args
bound(1);
bound(1, 2, 3);
ready
native partial
nativePartial(2);
nativePartial(2,3);
ready
simple partial
partial(2);
partial(2,3);
ready

Revisions

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

  • Revision 1: published by Justin on