Switch vs. hash vs. if else (v23)

Revision 23 of this benchmark created on


Preparation HTML

<script>
  var theVar = 10;

  var cases = [];
  cases[0] = function() {};
  cases[1] = function() {};
  cases[2] = function() {};
  cases[3] = function() {};
  cases[4] = function() {};
  cases[5] = function() {};
  cases[6] = function() {};
  cases[7] = function() {};
  cases[8] = function() {};
  cases[9] = function() {};
  cases[10] = function() {};
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Switch
var a1=  (function(){});

for (var i = 1; i < 100; i++) {
switch (i%10) {
case 0:
  a1();
  break;
case 1:
  a1();
  break;
case 2:
  a1();
  break;
case 3:
  a1();
  break;
case 4:
  a1();
  break;
case 5:
  a1();
  break;
case 6:
  a1();
  break;
case 7:
 a1();
  break;
case 8:
 a1();
  break;
case 9:
 a1();
  break;
case 10:
 a1();
  break;
default:
}
}
ready
Hash
for (var i = 1; i < 100; i++) {
//if (typeof cases[theVar] == 'function') {
cases[i%10]();
//} else {
//}
}
ready
If else
var a1=  (function(){});
var j;

for (var i = 1; i < 100; i++) {
j = i%10;
if (j=== 0)
  a1();
else if (j===  1)
  a1();
else if (j===  2)
  a1();
else if (j===   3)
  a1();
else if (j===   4)
  a1();
else if (j===   5)
  a1();
else if (j===   6)
  a1();
else if (j===   7)
 a1();
else if (j===   8)
 a1();
else if (j===   9)
 a1();
else if (j===   10)
 a1();
}
ready

Revisions

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