Simple null bind vs closure

Benchmark created on


Setup

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]()
    }
  }

Teardown



            A.length = 0;
  todo.length = 0;
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
bind
for (i = 1; i < 100; ++i) {
  plan(set.bind(null, i, 1.0 / (0.0 + i)))
}
go()
ready
closure
for (i = 1; i < 100; ++i) {
  plan(
    (function(j) {
      return function() {
        set(j, 1.0 / (0.0 + j))
      }
    })(i)
  )
}

go()
ready
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()
ready
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()
ready

Revisions

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