bind

JavaScript performance comparison

Test case created by arian

Preparation code

<script>
  Array.slice = Array.from = function(array, start, end){
       return Array.prototype.slice.call(array, start, end);
  };
 
  Function.prototype.bind1 = function(bind){
        var self = this,
                args = (arguments.length > 1) ? Array.slice(arguments, 1) : null;
 
        return function(){
                if (!args && !arguments.length) return self.call(bind);
                if (args && arguments.length) return self.apply(bind, args.concat(Array.from(arguments)));
                return self.apply(bind, args || arguments);
        };
  };
 
  Function.prototype.bind2 = function(that){
        var self = this,
                args = arguments.length > 1 ? Array.slice(arguments, 1) : null;
        var bound = function(){
                var bind, F;
                if (this instanceof bound){
                        F = function(){};
                        F.prototype = self.prototype;
                        bind = new F;
                } else bind = that;
                var result = (!args && !arguments.length) ? self.call(bind)
                        : (args && arguments.length) ? self.apply(bind, args.concat(Array.from(arguments)))
                        : self.apply(bind, args || arguments);
                return F ? bind : result;
        };
        return bound;
  };
 
  Function.prototype.bind3 = function(that){
      var self = this,
          args = arguments.length > 1 ? Array.slice(arguments, 1) : null,
          F = function(){};
 
      var bound = function(){
          var context = that, length = arguments,length;
          if (this instanceof bound){
              F.prototype = self.prototype;
              context = new F;
          }
          var result = (!args && !length)
              ? self.call(context)
              : self.apply(context, args && length ? args.concat(Array.slice(arguments)) : args || arguments);
          return context == that ? result : context;
      };
      return bound;
  };
 
  Function.prototype.bind4 = function(that){
      var self = this,
          args = arguments.length > 1 ? Array.slice(arguments, 1) : null;
 
      var bound = function(){
          var context = that, length = arguments,length;
          if (this instanceof bound){
              var F = function(){};
              F.prototype = self.prototype;
              context = new F;
          }
          var result = (!args && !length)
              ? self.call(context)
              : self.apply(context, args && length ? args.concat(Array.slice(arguments)) : args || arguments);
          return context == that ? result : context;
      };
      return bound;
  };
 
 
</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…
Improved
var F = fn.bind3(obj, 'foo', 'bar');
F();
pending…
lazy F
var F = fn.bind4(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