Switch vs. hash (v7)

Revision 7 of this benchmark created by Kevin Decker on


Preparation HTML

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

Test runner

Ready to run.

Testing in
TestOps/sec
Switch
switch (theVar) {
case 'a':
 'a';
 break;
case 'b':
 'b';
 break;
case 'c':
 'c';
 break;
case 'd':
 'd';
 break;
case 'e':
 'e'
 break;
case 'f':
 'f';
 break;
case 'g':
 'g';
 break;
case 'h':
 'h';
 break;
case 'i':
 'i';
 break;
case 'j':
 'j';
 break;
default:
 'default';
 break;
}
ready
Hash
if (cases[theVar]) {
 (cases[theVar])();
} else {
 'default';
}
ready
Falsy Default
(cases[theVar] || defaultFn)();
ready

Revisions

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