Simple null bind vs closure

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    A = [];
   
    function set(i, v) {
      A[i] = v;
    }
    todo = [];
   
    function plan(f) {
      todo.push(f);
    }
   
    function go() {
      var i = 0,
        l = todo.length;
      for (i = 0; i < l; ++i) {
        todo[i]()
      }
    }
};

Benchmark.prototype.teardown = function() {
    A.length = 0;
    todo.length = 0;
};
</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
for (i = 1; i < 100; ++i) {
  plan(set.bind(null, i, 1.0 / (0.0 + i)))
}
go()
pending…
closure
for (i = 1; i < 100; ++i) {
  plan(
    (function(j) {
      return function() {
        set(j, 1.0 / (0.0 + j))
      }
    })(i)
  )
}

go()
pending…
prototype-closure
Function.prototype.closure = function() {
  var func = this,
    a = arguments;
  return function() {
    func.apply(this, a);
  }
};

for (i = 1; i < 100; ++i) {
  plan(set.closure(i, 1.0 / (0.0 + i)))
}
go()
 
pending…
freeze-like-underscorejs-partial
function freeze(){ 
  var args = [].slice.call(arguments, 1), func = arguments[0];
  return function(){ func(args) }
}

for (i = 1; i < 100; ++i) {
  plan(freeze(set, i, 1.0 / (0.0 + i)))
}
go()
 
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