Switch vs. hash (v14)

Revision 14 of this benchmark created by Maciej Pawłowski on


Preparation HTML

<script>
  var theVar = 'j';
  
  var cases = [];
  cases['a'] = function() { var i = 1; i+=1; return i; };
  cases['b'] = function() { var i = 1; i+=1; return i; };
  cases['c'] = function() { var i = 1; i+=1; return i; };
  cases['d'] = function() { var i = 1; i+=1; return i; };
  cases['e'] = function() { var i = 1; i+=1; return i; };
  cases['f'] = function() { var i = 1; i+=1; return i; };
  cases['g'] = function() { var i = 1; i+=1; return i; };
  cases['h'] = function() { var i = 1; i+=1; return i; };
  cases['i'] = function() { var i = 1; i+=1; return i; };
  cases['j'] = function() { var i = 1; i+=1; return i; };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Switch
switch (theVar) {
case 'a':
 var i = 1; i+=1; 
 break;
case 'b':
 var i = 1; i+=1; 
 break;
case 'c':
 var i = 1; i+=1; 
 break;
case 'd':
 var i = 1; i+=1; 
 break;
case 'e':
 var i = 1; i+=1; 
 break;
case 'f':
 var i = 1; i+=1; 
 break;
case 'g':
 var i = 1; i+=1; 
 break;
case 'h':
 var i = 1; i+=1; 
 break;
case 'i':
 var i = 1; i+=1; 
 break;
case 'j':
 var i = 1; i+=1; 
 break;
default:
}
ready
Hash
if (typeof cases[theVar] === 'function') {
 cases[theVar]();
} else {

}
ready
Hash-cached
var fun = cases[theVar];

if (typeof fun === 'function') {
 fun();
} else {

}
ready
Try catch
try{
 cases[theVar]();
}
catch(err)
{
}
ready

Revisions

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