fast-apply vs apply

JavaScript performance comparison

Revision 2 of this test case created by Jayce Pulsipher

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var scope = { a: 1, b: 2, c: 3 }
    , oneArg = [ 1 ]
    , fiveArgs = [ 1, 2, 3, 4, 5 ]
    , sixArgs = [ 1, 2, 3, 4, 5, 6 ]
  ;
  function run() {}
  
  function fastApply(fn, scope, args) {
      
      switch (args ? args.length : 0) {
          case 0:
              return scope ? fn.call(scope) : fn();
          case 1:
              return scope ? fn.call(scope, args[0]) : fn(args[0]);
          case 2:
              return scope ? fn.call(scope, args[0], args[1]) : fn(args[0], args[1]);
          case 3:
              return scope ? fn.call(scope, args[0], args[1], args[2]) : fn(args[0], args[1], args[2]);
          case 4:
              return scope ? fn.call(scope, args[0], args[1], args[2], args[3]) : fn(args[0], args[1], args[2], args[3]);
          case 5:
              return scope ? fn.call(scope, args[0], args[1], args[2], args[3], args[4]) : fn(args[0], args[1], args[2], args[3], args[4]);
          default:
              return fn.apply(scope, args);
      }
      
  }

};
</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
apply (0 args)
run.apply(scope);
pending…
fastApply (0 args)
fastApply(run, scope);
pending…
fastApply (0 args, no scope)
fastApply(run, null);
pending…
apply (1 arg)
run.apply(scope, oneArg);
pending…
fastApply (1 args)
fastApply(run, scope, oneArg);
pending…
fastApply (1 args, no scope)
fastApply(run, null, oneArg);
pending…
apply (5 args)
run.apply(scope, fiveArgs);
pending…
fastApply (5 args)
fastApply(run, scope, fiveArgs);
pending…
fastApply (5 args, no scope)
fastApply(run, null, fiveArgs);
pending…
apply (6 args)
run.apply(scope, sixArgs);
pending…
fastApply (6 args)
fastApply(run, scope, sixArgs);
pending…
fastApply (6 args, no scope)
fastApply(run, null, sixArgs);
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