Switch vs. hash (v8)

Revision 8 of this benchmark created by layane on


Preparation HTML

<script>
  var theVar = 'j';
  
  var cases = {
   a: 'a',
   b: 'b',
   c: 'c',
   d: 'd',
   e: 'e',
   f: 'g',
   h: 'h',
   i: 'i',
   j: '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]) {
 'default';
}
ready
Falsy Default
(cases[theVar] || 'default');
ready

Revisions

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