Switch vs Hash vs Others (v25)

Revision 25 of this benchmark created by Trey Keown on


Preparation HTML

<script>
  var index = 0;
  var foo = 0;
  var cached = 0;

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

Setup

function hugeSwitch() {
    for (index = 0; index < 10; index++) {
    switch (index) {
    case 0:
      cases[0]();
      break;
    case 1:
      cases[1]();
      break;
    case 2:
      cases[2]();
      break;
    case 3:
      cases[3]();
      break;
    case 4:
      cases[4]();
      break;
    case 5:
      cases[5]();
      break;
    case 6:
      cases[6]();
      break;
    case 7:
      cases[7]();
      break;
    case 8:
      cases[8]();
      break;
    case 9:
      cases[9]();
      break;
    case 10:
      cases[10]();
      break;
    case 11:
      cases[11]();
      break;
    case 12:
      cases[12]();
      break;
    case 13:
      cases[13]();
      break;
    case 14:
      cases[14]();
      break;
    case 15:
      cases[15]();
      break;
    case 16:
      cases[16]();
      break;
    case 17:
      cases[17]();
      break;
    case 18:
      cases[18]();
      break;
    case 19:
      cases[19]();
      break;
    case 20:
      cases[20]();
      break;
    case 21:
      cases[21]();
      break;
    case 22:
      cases[22]();
      break;
    case 23:
      cases[23]();
      break;
    case 24:
      cases[24]();
      break;
    case 25:
      cases[25]();
      break;
    case 26:
      cases[26]();
      break;
    case 27:
      cases[27]();
      break;
    case 28:
      cases[28]();
      break;
    case 29:
      cases[29]();
      break;
    case 30:
      cases[30]();
      break;
    case 31:
      cases[31]();
      break;
    case 32:
      cases[32]();
      break;
    case 33:
      cases[33]();
      break;
    case 34:
      cases[34]();
      break;
    case 35:
      cases[35]();
      break;
    case 36:
      cases[36]();
      break;
    case 37:
      cases[37]();
      break;
    case 38:
      cases[38]();
      break;
    case 39:
      cases[39]();
      break;
    case 40:
      cases[40]();
      break;
    case 41:
      cases[41]();
      break;
    case 42:
      cases[42]();
      break;
    case 43:
      cases[43]();
      break;
    case 44:
      cases[44]();
      break;
    case 45:
      cases[45]();
      break;
    case 46:
      cases[46]();
      break;
    case 47:
      cases[47]();
      break;
    case 48:
      cases[48]();
      break;
    case 49:
      cases[49]();
      break;
    case 50:
      cases[50]();
      break;
    case 51:
      cases[51]();
      break;
    case 52:
      cases[52]();
      break;
    case 53:
      cases[53]();
      break;
    case 54:
      cases[54]();
      break;
    case 55:
      cases[55]();
      break;
    case 56:
      cases[56]();
      break;
    case 57:
      cases[57]();
      break;
    case 58:
      cases[58]();
      break;
    case 59:
      cases[59]();
      break;
    case 60:
      cases[60]();
      break;
    case 61:
      cases[61]();
      break;
    case 62:
      cases[62]();
      break;
    case 63:
      cases[3]();
      break;
    case 64:
      cases[64]();
      break;
    case 65:
      cases[65]();
      break;
    case 66:
      cases[66]();
      break;
    case 67:
      cases[67]();
      break;
    case 68:
      cases[68]();
      break;
    case 69:
      cases[69]();
      break;
    case 70:
      cases[70]();
      break;
    case 71:
      cases[71]();
      break;
    case 72:
      cases[72]();
      break;
    case 73:
      cases[73]();
      break;
    case 74:
      cases[74]();
      break;
    case 75:
      cases[75]();
      break;
    case 76:
      cases[76]();
      break;
    case 77:
      cases[77]();
      break;
    case 78:
      cases[78]();
      break;
    case 79:
      cases[79]();
      break;
    case 80:
      cases[80]();
      break;
    case 81:
      cases[81]();
      break;
    case 82:
      cases[82]();
      break;
    case 83:
      cases[83]();
      break;
    case 84:
      cases[84]();
      break;
    case 85:
      cases[85]();
      break;
    case 86:
      cases[6]();
      break;
    case 87:
      cases[87]();
      break;
    case 88:
      cases[88]();
      break;
    case 89:
      cases[89]();
      break;
    case 90:
      cases[90]();
      break;
    case 91:
      cases[91]();
      break;
    case 92:
      cases[92]();
      break;
    case 93:
      cases[93]();
      break;
    case 94:
      cases[94]();
      break;
    case 95:
      cases[95]();
      break;
    case 96:
      cases[96]();
      break;
    case 97:
      cases[97]();
      break;
    case 98:
      cases[98]();
      break;
    case 99:
      cases[99]();
      break;
    }
  }
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Switch
for (index = 0; index < 10; index++) {
  switch (index) {
  case 0:
    cases[0]();
    break;
  case 1:
    cases[1]();
    break;
  case 2:
    cases[2]();
    break;
  case 3:
    cases[3]();
    break;
  case 4:
    cases[4]();
    break;
  case 5:
    cases[5]();
    break;
  case 6:
    cases[6]();
    break;
  case 7:
    cases[7]();
    break;
  case 8:
    cases[8]();
    break;
  case 9:
    cases[9]();
    break;
  }
}
ready
Hash
for (index = 0; index < 10; index++) {
  cases[index]();
}
ready
Inline Switch
for (index = 0; index < 10; index++) {
  switch (index) {
  case 0:
    foo++;
    break;
  case 1:
    foo++;
    break;
  case 2:
    foo++;
    break;
  case 3:
    foo++;
    break;
  case 4:
    foo++;
    break;
  case 5:
    foo++;
    break;
  case 6:
    foo++;
    break;
  case 7:
    foo++;
    break;
  case 8:
    foo++;
    break;
  case 9:
    foo++;
    break;
  }
}
ready
Cached Hash
for (index = 0; index < 10; index++) {
  cached = cases[index];
  cached();
}
ready
If ===
for (index = 0; index < 10; index++) {
  if (index === 0) {
    cases[0]();
  } else if (index === 1) {
    cases[1]();
  } else if (index === 2) {
    cases[2]();
  } else if (index === 3) {
    cases[3]();
  } else if (index === 4) {
    cases[4]();
  } else if (index === 5) {
    cases[5]();
  } else if (index === 6) {
    cases[6]();
  } else if (index === 7) {
    cases[7]();
  } else if (index === 8) {
    cases[8]();
  } else if (index === 9) {
    cases[9]();
  }
}
ready
If ==
for (index = 0; index < 10; index++) {
  if (index == 0) {
    cases[0]();
  } else if (index == 1) {
    cases[1]();
  } else if (index == 2) {
    cases[2]();
  } else if (index == 3) {
    cases[3]();
  } else if (index == 4) {
    cases[4]();
  } else if (index == 5) {
    cases[5]();
  } else if (index == 6) {
    cases[6]();
  } else if (index == 7) {
    cases[7]();
  } else if (index == 8) {
    cases[8]();
  } else if (index == 9) {
    cases[9]();
  }
}
ready

Revisions

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