bind

JavaScript performance comparison

Revision 2 of this test case created

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 = new self;
                        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(bind){
        var self = this, args = Array.slice(arguments), args = (args.length > 1) ? args.slice(1) : null;
        return function bound(){
                if (!this instanceof bound) return bind;
               
                var F = function(){};
                F.prototype = new self;
                bind = new F;
 
                args = args.concat(Array.slice(arguments));
                return !args ? self.call(bind) : self.apply(bind, args);
        };
  };
 
 
  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…
New 2
var F = fn.bind3(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