function calls: direct vs apply vs call vs bind

JavaScript performance comparison

Revision 98 of this test case created by David

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  monkeyBind = function (self) {
      var fn = self,
          args = Array.prototype.slice.call(arguments),
          object = args.shift();
      return function () {
          return fn.apply(object,
          args.concat(Array.prototype.slice.call(arguments)));
      };
  };
  
  var f = function(foo, a, b, c) {
    return [foo, a, b, c];
  };
  
  var f2 = function(a, b, c) {
    return [this, a, b, c];
  };
  
  var foo = {};
  
  var a = 1,
    b = 2,
    c = 3,
    abc = [1, 2, 3];
  
  var bound = f2.bind(foo);
  
  var monkeyBound = monkeyBind(f2, foo)

};
</script>

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
direct
f(foo, a, b, c);
pending…
apply
f2.apply(foo, abc);
pending…
call
f2.call(foo, a, b, c);
pending…
bind
bound();
pending…
apply2
f2.apply(foo, [a, b, c])
pending…
monkeyBound
monkeyBound();
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