bind vs closure (v2)

Revision 2 of this benchmark created by x on


Preparation HTML

<script>
  if (!Function.prototype.bind) {
    Function.prototype.bind = function(context) {
      var fn = this;
      return function() {
        return fn.apply(context, arguments);
      }
    }
  }
  var obj = {
    c: function() {
      return Math.sin(1);
    }
  }

  obj.closure = function(cb) {
    var that = this;
    setTimeout(function() {
      that.c();
      cb();
    }, 0);
  }

  obj.bind = function(cb) {
    setTimeout((function() {
      this.c();
      cb();
    }).bind(this), 0);
  }

  obj.cached = (function(cb) {
    var f = (function(cb) {
      this.c();
      cb();
    }).bind(obj);

    return function(cb) {
      setTimeout(f, 0, cb);
    }
  })();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
closure
// async test
obj.closure(function() {
  deferred.resolve();
});
ready
bind
// async test
obj.bind(function() {
  deferred.resolve();
});
ready
cached
// async test
obj.cached(function() {
  deferred.resolve();
})
ready

Revisions

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