if vs else if

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function howBig1( num ) {
      if ( num < 10 ) {
        return 'small';
      } else if ( num >= 10 && num < 100 ) {
        return 'medium';
      } else if ( num >= 100 ) {
        return 'big';
      }
    }
   
    function howBig2( num ) {
      if ( num < 10 ) {
        return 'small';
      }
   
      if ( num < 100 ) {
        return 'medium';
      }
   
      if ( num >= 100 ) {
        return 'big';
      }
    }
   
   
    function howBig3( num ) {
      if ( num < 10 ) {
        return 'small';
      }
   
      if ( num < 100 ) {
        return 'medium';
      }
   
      return 'big';
    }
   
   
    function howBig4( num ) {
   
    if (num >= 100){return 'big';}
   
    return (num < 10) ? 'small' : 'medium';
   
    };
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Else If
howBig1();
pending…
If
howBig2();
pending…
Simplified If
howBig3();
pending…
If with ternary operator
howBig4();
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment