casevsswitch

JavaScript performance comparison

Revision 7 of this test case created

Preparation code

<script>
  var array = [
"The string is for case 'a'",
"The string is for case 'b'",
"The string is for case 'c'",
"The string is for case 'd'",
"The string is for case 'e'",
"The string is for case 'f'",
"The string is for case 'g'",
"The string is for case 'h'",
"The string is for case 'i'",
"The string is for case 'j'"];
 
 
  var objDictionary = {
    'a' : "The string is for case 'a'",
    'b' : "The string is for case 'b'",
    'c' : "The string is for case 'c'",
    'd' : "The string is for case 'd'",
    'e' : "The string is for case 'e'",
    'f' : "The string is for case 'f'",
    'g' : "The string is for case 'g'",
    'h' : "The string is for case 'h'",
    'i' : "The string is for case 'i'",
    'j' : "The string is for case 'j'"
  }
 
  var objFuncDictionary = {
    'a' : function() {return "The string is for case 'a'";},
    'b' : function() {return "The string is for case 'b'";},
    'c' : function() {return "The string is for case 'c'";},
    'd' : function() {return "The string is for case 'd'";},
    'e' : function() {return "The string is for case 'e'";},
    'f' : function() {return "The string is for case 'f'";},
    'g' : function() {return "The string is for case 'g'";},
    'h' : function() {return "The string is for case 'h'";},
    'i' : function() {return "The string is for case 'i'";},
    'j' : function() {return "The string is for case 'j'";},
  }

var arrayFns = [
    function() {return "The string is for case 'a'";},
    function() {return "The string is for case 'b'";},
    function() {return "The string is for case 'c'";},
    function() {return "The string is for case 'd'";},
    function() {return "The string is for case 'e'";},
    function() {return "The string is for case 'f'";},
    function() {return "The string is for case 'g'";},
    function() {return "The string is for case 'h'";},
    function() {return "The string is for case 'i'";},
    function() {return "The string is for case 'j'";},
  ]


function switchLookup(strKey) {
switch( strKey ) {
  case 'a' :
    return "The string is for case 'a'";
    break;
   
  case 'b' :
    return "The string is for case 'b'";
    break;
   
  case 'c' :
    return "The string is for case 'c'";
    break;
   
  case 'd' :
    return "The string is for case 'd'";
    break;
   
  case 'e' :
    return "The string is for case 'e'";
    break;
   
  case 'f' :
    return "The string is for case 'f'";
    break;
   
  case 'g' :
    return "The string is for case 'g'";
    break;
   
  case 'h' :
    return "The string is for case 'h'";
    break;
   
  case 'i' :
    return "The string is for case 'i'";
    break;
   
  case 'j' :
    return "The string is for case 'j'";
    break;
}
}

var rands = [], randc = [];
for(var i = 0; i < 100; i++) {
rands[i] = ~~(Math.random()*10);
randc[i] = String.fromCharCode(97 + rands[i]);
}
</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
Lookup by object
for(var i = 0; i < 100; i++) { 
strValue = objDictionary[randc[i]];
}
pending…
Lookup by switch
for(var i = 0; i < 100; i++) { 
switchLookup(randc[i]);
}
pending…
Lookup by array
for(var i = 0; i < 100; i++) { 
strValue = array[rands[i]];
}
pending…
Lookup by object function
for(var i = 0; i < 100; i++) { 
strValue = objFuncDictionary[randc[i]]();
}
pending…
Lookup by array function
for(var i = 0; i < 100; i++) { 
strValue = arrayFns[rands[i]]();
}
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

fred commented :

any good compiler can weed out a simple constant lookup.

therefore, randomize a 100 entries and then try across a disparate range of attempts.

the object function was also... not invoking the function.

also, add better "array" lookup for bytecode style usage, and add "array" function calls, as the simplistic string return case is least likely to be useful.

Add a comment