Optional parameters perf

JavaScript performance comparison

Revision 2 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function fn1(arg1, arg2, arg3) {
      switch(arguments.length) {
          case 2:
              arg3 = 'sample';
          case 1:
              arg2 = 310;
          case 0:
              arg1 = /five/;
      }
  }
  
  function fn2(arg1, arg2, arg3) {
      if(arg1 === undefined) arg1 = /five/;
      if(arg2 === undefined) arg2 = 310;
      if(arg3 === undefined) arg3 = 'sample'
  }
  
  function fn3(arg1, arg2, arg3) {
      if(typeof arg1 === 'undefined') arg1 = /five/;
      if(typeof arg2 === 'undefined') arg2 = 310;
      if(typeof arg3 === 'undefined') arg3 = 'sample'
  }
  
  function fn4(arg1, arg2, arg3) {
      if(arguments.length === 0) arg1 = /five/;
      if(arguments.length <= 1) arg2 = 310;
      if(arguments.length <= 2) arg3 = 'sample'
  }

};
</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
Switch
fn1();
fn1('');
fn1('', 5);
pending…
If-Undefined
fn2();
fn2('');
fn2('', 5);
pending…
If-Typeof-Undefined
fn3();
fn3('');
fn3('', 5);
pending…
Arg-length
fn4();
fn4('');
fn4('', 5);
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