Function.bind performance

JavaScript performance comparison

Revision 5 of this test case created by Dan and last updated

Preparation code

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


Function.prototype.bindSimplified = function(context){
    var fn = this;
    return function(){
        return this instanceof fn ? fn() : fn.call(context, arguments[0]);
    };
};

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() {

      },
      testfunc1 = function() {
    
      };
;

  
  var first = testfunc.bindFirst(testcxt);
  var second = testfunc.secondBind(testcxt);
  var third = testfunc.bindThird(testcxt);
  var native = testfunc.bind ? testfunc.bind(testcxt) : testfunc.secondBind(testcxt);
  var simple = testfunc.bindSimplified(testcxt);

</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
First
first(testarg3)
pending…
Second
second(testarg3)
pending…
Third
third(testarg3)
pending…
Native
native(testarg3)
pending…
Simplified
simple(testarg3); //just to test "light" variant
pending…

Compare results of other browsers

Revisions

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

0 Comments