Bind vs That

JavaScript performance comparison

Revision 4 of this test case created

Info

Checking the performance of using _.bind vs "that = this"

Preparation code

<script src="http://underscorejs.org/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var stuff = {
      foo: 42,
      bar: 69
    };
};
</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
_.bind
function doStuff(){
  var baz = 0;
  for(var i=0;i<=10;i++){
    baz += i % 2 ? this.foo : this.bar;
  }
  return baz;
}

var z = _.bind(doStuff, stuff)();
pending…
that=this
var that = stuff;
function doStuff(){
  var baz = 0;
  for(var i=0;i<=10;i++){
    baz += i % 2 ? that.foo : that.bar;
  }
  return baz;
}

var z = doStuff();
pending…
native bind
function doStuff(){
  var baz = 0;
  for(var i=0;i<=10;i++){
    baz += i % 2 ? this.foo : this.bar;
  }
  return baz;
}

var z = doStuff.bind(stuff)();
pending…
.call
var obj = stuff;
function doStuff(){
  var baz = 0;
  for(var i=0;i<=10;i++){
    baz += i % 2 ? this.foo : this.bar;
  }
  return baz;
}

var z = doStuff.call(obj);
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