Function.bind performance

Benchmark created by vamp on


Preparation HTML

<script>
var slice = Array.prototype.slice,
    push = Array.prototype.push;

Function.prototype['bindFirst'] = function(context){
    var fn = this, args = slice.call(arguments,1);
    return function(){
        return fn.apply(context, args.concat(slice.call(arguments)));
    };
};

Function.prototype['secondBind'] = function(context){
    var fn = this, linked = [];
    push.apply(linked, arguments);
    linked.shift();

    return function(){
       var args = [];
       push.apply(args, linked);
       push.apply(args, arguments);
       return fn.apply(context, args);
    };
};

Function.prototype['bindThird'] = function (object) {
    var originalFunction = this, args = slice.call(arguments), object = args.shift();
    return function () {
       return originalFunction.apply(object, args.concat(slice.call(arguments)));
    }; 
};

// test function and context
  var testcxt = {},
      testarg1 = 'test',
      testarg2 = {},
      testarg3 = 1,
      testarg4 = {},
      testfunc = function() {
    if (this !== testcxt || arguments.length != 4) throw new Error('failed');
      };

  
  var first = testfunc.bindFirst(testcxt, testarg1, testarg2);
  var second = testfunc.secondBind(testcxt, testarg1, testarg2);
  var third = testfunc.bindThird(testcxt, testarg1, testarg2);
  var native = testfunc.bind ? testfunc.bind(testcxt, testarg1, testarg2) : testfunc.secondBind(testcxt, testarg1, testarg2);


</script>

Test runner

Ready to run.

Testing in
TestOps/sec
First
first(testarg3, testarg4)
ready
Second
second(testarg3, testarg4)
ready
Third
third(testarg3, testarg4)
ready
Native
native(testarg3, testarg4)
ready

Revisions

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