bind (v3)

Revision 3 of this benchmark created by John-David Dalton on


Setup

(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;
  }());
  
  

  Benchmark.prototype.setup = function() {
    var fn = function(type) {
      this.type = type;
    };
    var obj = {
      'type': 'bar'
    };
  };

Test runner

Ready to run.

Testing in
TestOps/sec
Native
var F = fn.bind(obj, 'foo', 'bar');
F();
 
ready
Old
var F = fn.bind1(obj, 'foo', 'bar');
F();
 
ready
New
var F = fn.bind2(obj, 'foo', 'bar');
F();
 
ready

Revisions

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

  • Revision 3: published by John-David Dalton on