switch-if + bottom

JavaScript performance comparison

Revision 5 of this test case created by tomByrer

Info

Forked v1; added repeated values to see if evaluation ordering is an influence.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var OPTIONS = [ "i", "f", "v2", "v3", "v4", "c", "fv1", "fv", "i", "f", "v2", "v3", "v2v", "i", "v3v", "v4v", "m4", "m4v", "t", "i", "f", "v2", "v3", "tv" ];
    var N = OPTIONS.length;
    function randomValue() {
        return OPTIONS[ Math.floor( Math.random() * N ) ];
    }
   
    var OPTIONS_BOTTOM = [ "i", "m4", "m4v", "t", "tv", "f", "v2", "v3", "m4", "m4v", "t", "tv", "v4", "tv", "c", "fv1", "fv", "v2v", "v3v", "v4v", "m4", "m4v", "t", "tv" ];
    var N_B = OPTIONS_BOTTOM.length;
    function randomValueBottom() {
        return OPTIONS_BOTTOM[ Math.floor( Math.random() * N_B ) ];
    }
};
</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
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…
if-else-b
var v = randomValueBottom();
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-b
var v = randomValueBottom();
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. Here’s a list of current revisions for this page:

1 comment

tomByrer (revision owner) commented :

To help visualize the array differences:

OPTIONS.sort() ["c", "f", "f", "f", "fv", "fv1", "i", "i", "i", "i", "m4", "m4v", "t", "tv", "v2", "v2", "v2", "v2v", "v3", "v3", "v3", "v3v", "v4", "v4v"]

OPTIONS_BOTTOM.sort() ["c", "f", "fv", "fv1", "i", "m4", "m4", "m4", "m4v", "m4v", "m4v", "t", "t", "t", "tv", "tv", "tv", "tv", "v2", "v2v", "v3", "v3v", "v4", "v4v"]

Pivoted results on my 1st-gen i7 Laptop: [{"UserAgent ":"Chrome 34.0.1847","if-else ":"24,317,389","switch ":"25,110,152","if-else-b ":"23,955,218","switch-b ":"23,955,218","# Tests ":2}, {"UserAgent ":"Firefox 30.0","if-else ":"10,566,795","switch ":"10,931,194","if-else-b ":"9,934,427","switch-b ":"9,934,427","# Tests ":1}, {"UserAgent ":"IE 10.0","if-else ":"5,101,449","switch ":"5,101,449","if-else-b ":"3,910,148","switch-b ":"3,919,598","# Tests ":2}, {"UserAgent ":"Pale Moon (Firefox Variant) 24.5.0","if-else ":"696.777","switch ":"736.277","if-else-b ":"696.777","switch-b ":"736.277","# Tests ":2}]

Conclusion: Ordering the evaluations most likely to be true near top is fastest or among fastest. switch-case may be slightly faster than if-else, requires less typing, & it reads easier.

Add a comment