switch-if

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var OPTIONS = [ "i", "f", "v2", "v3", "v4", "c", "fv1", "fv", "v2v", "v3v", "v4v", "m4", "m4v", "t", "tv" ];
      
  var N = OPTIONS.length;
  
  function randomValue() {
  
      return OPTIONS[ Math.floor( Math.random() * N ) ];
  
  }
  
  

};
</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
if-else
var v = randomValue();
var r = -1;

if ( v === "i" ) {

    r = 0;

} else if ( v === "f" ) {

    r = 1;

} else if ( v === "v2" ) {

    r = 2;

} else if ( v === "v3" ) {

    r = 3;

} else if ( v === "v4" ) {

    r = 4;

} else if ( v === "c" ) {

    r = 5;

} else if ( v === "fv1" ) {

    r = 6;

} else if ( v === "fv" ) {

    r = 7;

} else if ( v === "v2v" ) {

    r = 8;

} else if ( v === "v3v" ) {

    r = 9;

} else if ( v === "v4v" ) {

    r = 10;

} else if ( v === "m4" ) {

    r = 11;

} else if ( v === "m4v" ) {

    r = 12;

} else if ( v === "t" ) {

    r = 13;

} else if ( v === "tv" ) {

    r = 14;

}
pending…
switch
var v = randomValue();
var r = -1;

switch ( v ) {

    case "i" : r = 0; break;
    case "f" : r = 1; break;
    case "v2" : r = 2; break;
    case "v3" : r = 3; break;
    case "v4" : r = 4; break;
    case "c" : r = 5; break;
    case "fv1" : r = 6; break;
    case "fv" : r = 7; break;
    case "v2v" : r = 8; break;
    case "v3v" : r = 9; break;
    case "v4v" : r = 10; break;
    case "m4" : r = 11; break;
    case "m4v" : r = 12; break;
    case "t" : r = 13; break;
    case "tv" : r = 14; break;

}
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