Switch vs IF over true condition

JavaScript performance comparison

Test case created by EtnasSoft

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var foo = (function(){
    return {
      test : function() {
          console.log('entro en test');
          return true;
      },
      test2 : function () {
          return true;
      },
      notest : function () {
          return false;
      }
    }  
  })();

};
</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
switch ( true ) {  
  case foo.test():
    console.log( 'a.test is true. Break here...' );
    break;
  case foo.test2():
    console.log( 'a.test2 is true. Break here...' );
    break;
  default:
    console.log( 'All above is false.' );
    break;
}
pending…
If
if ( foo.test() ) {
  console.log( 'a.test is true. Break here...' );
} else if ( foo.test2() ) {
  console.log( 'a.test2 is true. Break here...' );
} else {
  console.log( 'All above is false.' );
}
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