bind vs closure

JavaScript performance comparison

Revision 10 of this test case created by Arthur

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/dojo/1/dojo/dojo.xd.js">
</script>
<script>
  function foo() {}
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var binder = function(scope, method) {
      return function() {
        return method.apply(scope, arguments);
      }
      },
      target = {};
  
  var test1 = function() {
      return this;
      }.bind(target);
  
  var test2 = function() {
      return target;
      };
  
  var test3 = dojo.hitch(target, function() {
    return this;
  });
  
  var test4 = binder(target, function() {
    return this;
  });
  
  var test5 = function() {
      return this;
      };

};
</script>

Preparation code output

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
bind()
test1();
pending…
self
test2();
pending…
dojo.hitch
test3();
pending…
test4();
pending…
call (control)
test5.call(target);
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