Simple null bind vs closure (v3)

Revision 3 of this 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(undefined, 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 fn = this;
    var context = arguments[0];
    var args = Array.prototype.slice.call(arguments, 1);

    return function() {
        var newArgs = Array.prototype.slice.call(arguments);
        return fn.apply(context, args.concat(newArgs));
    };
};

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.