casevsswitch

JavaScript performance comparison

Revision 5 of this test case created by Chris G

Preparation code

<script>
  var aDictionary = []
  aDictionary['a'] = "The string is for case 'a'";
  aDictionary['b'] = "The string is for case 'b'";
  aDictionary['c'] = "The string is for case 'c'";
  aDictionary['d'] = "The string is for case 'd'";
  aDictionary['e'] = "The string is for case 'e'";
  aDictionary['f'] = "The string is for case 'f'";
  aDictionary['g'] = "The string is for case 'g'";
  aDictionary['h'] = "The string is for case 'h'";
  aDictionary['i'] = "The string is for case 'i'";
  aDictionary['j'] = "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'";},
  }
</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
strValue = objDictionary['j'];
 
pending…
Lookup by switch
strKey = 'j';
switch( strKey ) {
  case 'a' :
    strResult = "The string is for case 'a'";
    break;
   
  case 'b' :
    strResult = "The string is for case 'b'";
    break;
   
  case 'c' :
    strResult = "The string is for case 'c'";
    break;
   
  case 'd' :
    strResult = "The string is for case 'd'";
    break;
   
  case 'e' :
    strResult = "The string is for case 'e'";
    break;
   
  case 'f' :
    strResult = "The string is for case 'f'";
    break;
   
  case 'g' :
    strResult = "The string is for case 'g'";
    break;
   
  case 'h' :
    strResult = "The string is for case 'h'";
    break;
   
  case 'i' :
    strResult = "The string is for case 'i'";
    break;
   
  case 'j' :
    strResult = "The string is for case 'j'";
    break;
}
pending…
Lookup by array
strValue = aDictionary['j'];
pending…
Lookup by object function
strValue = objFuncDictionary['j'];
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

Chris G (revision owner) commented :

Hmm, interesting.

Add a comment