Switch vs. hash (v18)

Revision 18 of this benchmark created 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; };
</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;
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.