bind

JavaScript performance comparison

Revision 3 of this test case created by John-David Dalton

Preparation code

<script>
  (function() {
    var slice = [].slice;
 
    function bind1(thisArg) {
      var fn = this,
          args = (arguments.length > 1) ? slice.call(arguments, 1) : null;
 
      return function() {
        var length = arguments.length;
        return (!args && !length)
          ? fn.call(thisArg)
          : fn.apply(thisArg, args && length ? args.concat(slice.call(arguments)) : args || arguments);
      };
    }
 
    function bind2(thisArg) {
      var fn = this,
          args = arguments.length > 1 ? slice.call(arguments, 1) : null;
 
      function F() { }
 
      function bound(){
        var result,
            context = thisArg,
            length = arguments.length;
 
        if (this instanceof bound){
          F.prototype = fn.prototype;
          context = new F;
        }
        result = (!args && !length)
          ? fn.call(context)
          : fn.apply(context, args && length ? args.concat(slice.call(arguments)) : args || arguments);
        return context == thisArg ? result : context;
      }
      return bound;
    }
 
    Function.prototype.bind1 = bind1;
    Function.prototype.bind2 = bind2;
  }());
 
 
</script>
<script>
Benchmark.prototype.setup = function() {
    var fn = function(type) {
      this.type = type;
    };
    var obj = {
      'type': 'bar'
    };
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Native
var F = fn.bind(obj, 'foo', 'bar');
F();
 
pending…
Old
var F = fn.bind1(obj, 'foo', 'bar');
F();
 
pending…
New
var F = fn.bind2(obj, 'foo', 'bar');
F();
 
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. Here’s a list of current revisions for this page:

0 comments

Add a comment