bind vs closure

JavaScript performance comparison

Test case created by

Preparation code

<script>
  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

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
closure
// async test
obj.closure(function() {
  deferred.resolve();
});
pending…
bind
// async test
obj.bind(function() {
  deferred.resolve();
});
pending…
cached
// async test
obj.cached(function() {
  deferred.resolve();
})
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.

0 Comments