Function.bind performance (v3)

Revision 3 of this benchmark created by Justin on


Setup

var bindImpl = Function.prototype.bind ||
      function(oThis) {
          var aArgs = Array.prototype.slice.call(arguments, 1),
              fToBind = this,
              fNOP = function() { },
              fBound = function() {
                  return fToBind.apply(this instanceof fNOP && oThis ?
                          this :
                          oThis,
                      aArgs.concat(Array.prototype.slice.call(arguments))
                  );
              };
          fNOP.prototype = this.prototype;
          fBound.prototype = new fNOP();
          return fBound;
      };
  
  var native = function() {
      this.label = "hello";
      this.stringAppend = function() {
          return this.label + "world";
      }
  };
  var nativeInstance = new native();
        
  var proto = function() {
      this.label = "hello";
  };        
  proto.prototype.stringAppend = function() {
      return this.label + "world";
  };
  var protoInstance = new proto();
  
  var bindStringAppend = function() {
      return this.label + "world";
  };
  var thisObj = function() {
      this.label = "hello";
      this.stringAppend = bindImpl.call(bindStringAppend, this);
  };
  var boundImpl = new thisObj();
  
  var closure = function() {
      this.label = "hello";
      var self = this;
      self.stringAppend = function() {
          return this.label + "world";
      };
  };
  var closureInstance = new closure();  
     
  // test function and context
  var nativeImpl = nativeInstance.stringAppend;
  var protoImpl = protoInstance.stringAppend;
  var closureImpl = closureInstance.stringAppend;
  var boundImpl = boundImpl.stringAppend;

Test runner

Ready to run.

Testing in
TestOps/sec
Native
nativeImpl();
ready
Proto
protoImpl();
ready
Closure
closureImpl();
ready
Bind
boundImpl();
ready

Revisions

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