Switch vs. hash (v2)

Revision 2 of this benchmark created by David Calhoun on


Preparation HTML

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

Test runner

Ready to run.

Testing in
TestOps/sec
Switch
switch (theVar) {
case 'a':

 (function() {});
 break;
case 'b':

 (function() {});
 break;
case 'c':

 (function() {});
 break;
case 'd':

 (function() {});
 break;
case 'e':

 (function() {});
 break;
case 'f':

 (function() {});
 break;
case 'g':

 (function() {});
 break;
case 'h':

 (function() {})
 break;
case 'i':

 (function() {});
 break;
case 'j':

 (function() {});
 break;
default:

 (function() {});
 break;
}
ready
Hash
if (cases[theVar]) {
 (cases[theVar])();
} else {
 (function() {});
}
ready

Revisions

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