Switch vs Hash vs Others (v41)

Revision 41 of this benchmark created on


Preparation HTML

<script>
  var foo = -1000;
var cases = new Array(22);
var ch = "a".charCodeAt(0);
for(var i = 0; i <= 21; i++) {
  cases[i] = new Function("foo+=" + i);
}
window.strthing = "";
</script>

Setup

var index = ~~(Math.random() * 22);
  var chr = "a".charCodeAt(0);
  var str = String.fromCharCode(chr + index);
  
  var actions = cases;
  
  var doSwitch = function() {
  switch (str) {
    case "a":
      foo+=0;
      break;
    case "b":
      foo+=1;
      break;
    case "c":
      foo+=2;
      break;
    case "d":
      foo+=3;
      break;
    case "e":
      foo+=4;
      break;
    case "f":
      foo+=5;
      break;
    case "g":
      foo+=6;
      break;
    case "h":
      foo+=7;
      break;
    case "i":
      foo+=8;
      break;
    case "j":
      foo+=9;
      break;
    case "k":
      foo+=10;
      break;
    case "l":
      foo+=11;
      break;
    case "m":
      foo+=12;
      break;
    case "n":
      foo+=13;
      break;
    case "o":
      foo+=14;
      break;
    case "p":
      foo+=15;
      break;
    case "q":
      foo+=16;
      break;
    case "r":
      foo+=17;
      break;
    case "s":
      foo+=18;
      break;
    case "t":
      foo+=19;
      break;
    case "u":
      foo+=20;
      break;
    case "v":
      foo+=21;
      break;
    default:
      ;
  };
  if(foo > 1000) {window.strthing=foo;foo=foo%10;}
  };
  var doHash = function() {
  actions[index]();
  if(foo > 1000) {window.strthing=foo;foo=foo%10;}
  };

Test runner

Ready to run.

Testing in
TestOps/sec
Switch
doSwitch();
ready
Hash
doHash();
ready

Revisions

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