Switch vs Hash vs Others Less than 128 (v44)

Revision 44 of this benchmark created by tomplowman on


Preparation HTML

<script>
var foo = 0;
</script>

Setup

foo = 0;
  var curTest = 0;
  var actions = {
  0: function() { foo += 238;},
  1: function() { foo += 20;},
  2: function() { foo += 228;},
  3: function() { foo += 130;},
  4: function() { foo += 33;},
  5: function() { foo += 55;},
  6: function() { foo += 88;},
  7: function() { foo += 3;},
  8: function() { foo += 170;},
  9: function() { foo += 49;},
  10: function() { foo += 18;},
  11: function() { foo += 88;},
  12: function() { foo += 117;},
  13: function() { foo += 216;},
  14: function() { foo += 106;},
  15: function() { foo += 131;},
  16: function() { foo += 138;},
  17: function() { foo += 165;},
  18: function() { foo += 90;},
  19: function() { foo += 216;},
  20: function() { foo += 154;},
  21: function() { foo += 203;},
  22: function() { foo += 6;},
  23: function() { foo += 74;},
  24: function() { foo += 24;},
  25: function() { foo += 66;},
  26: function() { foo += 49;},
  27: function() { foo += 22;},
  28: function() { foo += 83;},
  29: function() { foo += 234;},
  30: function() { foo += 192;},
  31: function() { foo += 174;},
  32: function() { foo += 234;},
  33: function() { foo += 13;},
  34: function() { foo += 193;},
  35: function() { foo += 186;},
  36: function() { foo += 125;},
  37: function() { foo += 68;},
  38: function() { foo += 217;},
  39: function() { foo += 100;},
  40: function() { foo += 237;},
  41: function() { foo += 32;},
  42: function() { foo += 181;},
  43: function() { foo += 4;},
  44: function() { foo += 145;},
  45: function() { foo += 237;},
  46: function() { foo += 189;},
  47: function() { foo += 6;},
  48: function() { foo += 129;},
  49: function() { foo += 108;},
  50: function() { foo += 219;},
  51: function() { foo += 50;},
  52: function() { foo += 206;},
  53: function() { foo += 93;},
  54: function() { foo += 120;},
  55: function() { foo += 91;},
  56: function() { foo += 21;},
  57: function() { foo += 164;},
  58: function() { foo += 32;},
  59: function() { foo += 184;},
  60: function() { foo += 82;},
  61: function() { foo += 126;},
  62: function() { foo += 170;},
  63: function() { foo += 72;},
  64: function() { foo += 108;},
  65: function() { foo += 0;},
  66: function() { foo += 200;},
  67: function() { foo += 155;},
  68: function() { foo += 129;},
  69: function() { foo += 150;},
  70: function() { foo += 105;},
  71: function() { foo += 117;},
  72: function() { foo += 229;},
  73: function() { foo += 4;},
  74: function() { foo += 201;},
  75: function() { foo += 202;},
  76: function() { foo += 173;},
  77: function() { foo += 190;},
  78: function() { foo += 39;},
  79: function() { foo += 188;},
  80: function() { foo += 177;},
  81: function() { foo += 9;},
  82: function() { foo += 167;},
  83: function() { foo += 222;},
  84: function() { foo += 48;},
  85: function() { foo += 198;},
  86: function() { foo += 121;},
  87: function() { foo += 16;},
  88: function() { foo += 103;},
  89: function() { foo += 70;},
  90: function() { foo += 56;},
  91: function() { foo += 75;},
  92: function() { foo += 94;},
  93: function() { foo += 108;},
  94: function() { foo += 58;},
  95: function() { foo += 42;},
  96: function() { foo += 123;},
  97: function() { foo += 107;},
  98: function() { foo += 31;},
  99: function() { foo += 99;},
  100: function() { foo += 213;},
  101: function() { foo += 98;},
  102: function() { foo += 88;},
  103: function() { foo += 229;},
  104: function() { foo += 169;},
  105: function() { foo += 160;},
  106: function() { foo += 14;},
  107: function() { foo += 217;},
  108: function() { foo += 172;},
  109: function() { foo += 170;},
  110: function() { foo += 246;},
  111: function() { foo += 93;},
  112: function() { foo += 217;},
  113: function() { foo += 18;},
  114: function() { foo += 229;},
  115: function() { foo += 18;},
  116: function() { foo += 68;},
  117: function() { foo += 28;},
  118: function() { foo += 99;},
  119: function() { foo += 194;},
  120: function() { foo += 240;},
  121: function() { foo += 210;},
  122: function() { foo += 79;},
  123: function() { foo += 151;},
  124: function() { foo += 185;},
  125: function() { foo += 88;},
  126: function() { foo += 75;}
  };
  
  var doSwitch = function() {
  switch(curTest) {
  case 0:
  foo += 247;break;
  case 1:
  foo += 100;break;
  case 2:
  foo += 227;break;
  case 3:
  foo += 49;break;
  case 4:
  foo += 63;break;
  case 5:
  foo += 0;break;
  case 6:
  foo += 189;break;
  case 7:
  foo += 180;break;
  case 8:
  foo += 216;break;
  case 9:
  foo += 196;break;
  case 10:
  foo += 214;break;
  case 11:
  foo += 13;break;
  case 12:
  foo += 233;break;
  case 13:
  foo += 162;break;
  case 14:
  foo += 137;break;
  case 15:
  foo += 109;break;
  case 16:
  foo += 184;break;
  case 17:
  foo += 46;break;
  case 18:
  foo += 51;break;
  case 19:
  foo += 57;break;
  case 20:
  foo += 166;break;
  case 21:
  foo += 184;break;
  case 22:
  foo += 174;break;
  case 23:
  foo += 122;break;
  case 24:
  foo += 13;break;
  case 25:
  foo += 22;break;
  case 26:
  foo += 226;break;
  case 27:
  foo += 182;break;
  case 28:
  foo += 22;break;
  case 29:
  foo += 112;break;
  case 30:
  foo += 241;break;
  case 31:
  foo += 17;break;
  case 32:
  foo += 140;break;
  case 33:
  foo += 139;break;
  case 34:
  foo += 92;break;
  case 35:
  foo += 190;break;
  case 36:
  foo += 95;break;
  case 37:
  foo += 12;break;
  case 38:
  foo += 227;break;
  case 39:
  foo += 123;break;
  case 40:
  foo += 131;break;
  case 41:
  foo += 210;break;
  case 42:
  foo += 178;break;
  case 43:
  foo += 193;break;
  case 44:
  foo += 197;break;
  case 45:
  foo += 62;break;
  case 46:
  foo += 202;break;
  case 47:
  foo += 168;break;
  case 48:
  foo += 139;break;
  case 49:
  foo += 211;break;
  case 50:
  foo += 42;break;
  case 51:
  foo += 174;break;
  case 52:
  foo += 36;break;
  case 53:
  foo += 248;break;
  case 54:
  foo += 80;break;
  case 55:
  foo += 177;break;
  case 56:
  foo += 7;break;
  case 57:
  foo += 84;break;
  case 58:
  foo += 40;break;
  case 59:
  foo += 70;break;
  case 60:
  foo += 248;break;
  case 61:
  foo += 36;break;
  case 62:
  foo += 48;break;
  case 63:
  foo += 157;break;
  case 64:
  foo += 163;break;
  case 65:
  foo += 119;break;
  case 66:
  foo += 220;break;
  case 67:
  foo += 105;break;
  case 68:
  foo += 181;break;
  case 69:
  foo += 90;break;
  case 70:
  foo += 75;break;
  case 71:
  foo += 23;break;
  case 72:
  foo += 120;break;
  case 73:
  foo += 98;break;
  case 74:
  foo += 60;break;
  case 75:
  foo += 230;break;
  case 76:
  foo += 164;break;
  case 77:
  foo += 190;break;
  case 78:
  foo += 119;break;
  case 79:
  foo += 58;break;
  case 80:
  foo += 156;break;
  case 81:
  foo += 152;break;
  case 82:
  foo += 143;break;
  case 83:
  foo += 114;break;
  case 84:
  foo += 127;break;
  case 85:
  foo += 32;break;
  case 86:
  foo += 161;break;
  case 87:
  foo += 157;break;
  case 88:
  foo += 209;break;
  case 89:
  foo += 75;break;
  case 90:
  foo += 17;break;
  case 91:
  foo += 82;break;
  case 92:
  foo += 86;break;
  case 93:
  foo += 137;break;
  case 94:
  foo += 143;break;
  case 95:
  foo += 72;break;
  case 96:
  foo += 172;break;
  case 97:
  foo += 69;break;
  case 98:
  foo += 10;break;
  case 99:
  foo += 62;break;
  case 100:
  foo += 249;break;
  case 101:
  foo += 110;break;
  case 102:
  foo += 78;break;
  case 103:
  foo += 44;break;
  case 104:
  foo += 224;break;
  case 105:
  foo += 217;break;
  case 106:
  foo += 185;break;
  case 107:
  foo += 185;break;
  case 108:
  foo += 221;break;
  case 109:
  foo += 178;break;
  case 110:
  foo += 112;break;
  case 111:
  foo += 156;break;
  case 112:
  foo += 154;break;
  case 113:
  foo += 100;break;
  case 114:
  foo += 239;break;
  case 115:
  foo += 4;break;
  case 116:
  foo += 1;break;
  case 117:
  foo += 114;break;
  case 118:
  foo += 183;break;
  case 119:
  foo += 143;break;
  case 120:
  foo += 43;break;
  case 121:
  foo += 239;break;
  case 122:
  foo += 227;break;
  case 123:
  foo += 247;break;
  case 124:
  foo += 121;break;
  case 125:
  foo += 29;break;
  case 126:
  foo += 236;break;
  };
  if(foo > 1000) {window.strthing=foo;foo=0;} /* just to make sure we don't get optimized away */
  };
  
  var doIfElse = function() {
  
  if(curTest < 100) {
  
    if(curTest < 50) {
  
      if(curTest < 25) {
  
        switch(curTest) {
          case 0:
          foo += 247;break;
  
          case 1:
          foo += 43;break;
  
          case 2:
          foo += 2;break;
  
          case 3:
          foo += 345;break;
  
          case 4:
          foo += 43;break;
  
          case 5:
          foo += 889;break;
  
          case 6:
          foo += 65;break;
  
          case 7:
          foo += 4;break;
  
          case 8:
          foo += 15;break;
  
          case 9:
          foo += 64;break;
  
          case 10:
          foo += 34;break;
  
          case 11:
          foo += 97;break;
  
          case 12:
          foo += 34;break;
  
          case 13:
          foo += 65;break;
  
          case 14:
          foo += 23;break;
  
          case 15:
          foo += 99;break;
  
          case 16:
          foo += 99;break;
  
          case 17:
          foo += 209;break;
  
          case 18:
          foo += 34;break;
  
          case 19:
          foo += 48;break;
  
          case 20:
          foo += 33;break;
  
          case 21:
          foo += 155;break;
  
          case 22:
          foo += 896;break;
  
          case 23:
          foo += 34;break;
  
          case 24:
          foo += 53;break;
  
        }
  
      } else {
  
        switch(curTest) {
          case 25:
          foo += 247;break;
  
          case 26:
          foo += 247;break;
  
          case 27:
          foo += 43;break;
  
          case 28:
          foo += 2;break;
  
          case 29:
          foo += 345;break;
  
          case 30:
          foo += 43;break;
  
          case 31:
          foo += 889;break;
  
          case 32:
          foo += 65;break;
  
          case 33:
          foo += 4;break;
  
          case 34:
          foo += 15;break;
  
          case 35:
          foo += 64;break;
  
          case 36:
          foo += 34;break;
  
          case 37:
          foo += 97;break;
  
          case 38:
          foo += 34;break;
  
          case 39:
          foo += 65;break;
  
          case 40:
          foo += 23;break;
  
          case 41:
          foo += 99;break;
  
          case 42:
          foo += 209;break;
  
          case 43:
          foo += 34;break;
  
          case 44:
          foo += 48;break;
  
          case 45:
          foo += 33;break;
  
          case 46:
          foo += 155;break;
  
          case 47:
          foo += 896;break;
  
          case 48:
          foo += 34;break;
  
          case 49:
          foo += 53;break;
        }
  
      }
  
    } else {
  
      if(curTest < 75) {
  
        switch(curTest) {
          case 50:
          foo += 247;break;
  
          case 51:
          foo += 43;break;
  
          case 52:
          foo += 2;break;
  
          case 53:
          foo += 345;break;
  
          case 54:
          foo += 43;break;
  
          case 55:
          foo += 889;break;
  
          case 56:
          foo += 65;break;
  
          case 57:
          foo += 4;break;
  
          case 58:
          foo += 15;break;
  
          case 59:
          foo += 64;break;
  
          case 60:
          foo += 34;break;
  
          case 61:
          foo += 97;break;
  
          case 62:
          foo += 34;break;
  
          case 63:
          foo += 65;break;
  
          case 64:
          foo += 23;break;
  
          case 65:
          foo += 99;break;
  
          case 66:
          foo += 99;break;
  
          case 67:
          foo += 209;break;
  
          case 68:
          foo += 34;break;
  
          case 69:
          foo += 48;break;
  
          case 70:
          foo += 33;break;
  
          case 71:
          foo += 155;break;
  
          case 72:
          foo += 896;break;
  
          case 73:
          foo += 34;break;
  
          case 74:
          foo += 53;break;
        }
  
      } else {
  
        switch(curTest) {
          case 75:
          foo += 247;break;
  
          case 76:
          foo += 247;break;
  
          case 77:
          foo += 43;break;
  
          case 78:
          foo += 2;break;
  
          case 79:
          foo += 345;break;
  
          case 80:
          foo += 43;break;
  
          case 81:
          foo += 889;break;
  
          case 82:
          foo += 65;break;
  
          case 83:
          foo += 4;break;
  
          case 84:
          foo += 15;break;
  
          case 85:
          foo += 64;break;
  
          case 86:
          foo += 34;break;
  
          case 87:
          foo += 97;break;
  
          case 88:
          foo += 34;break;
  
          case 89:
          foo += 65;break;
  
          case 90:
          foo += 23;break;
  
          case 91:
          foo += 99;break;
  
          case 92:
          foo += 209;break;
  
          case 93:
          foo += 34;break;
  
          case 94:
          foo += 48;break;
  
          case 95:
          foo += 33;break;
  
          case 96:
          foo += 155;break;
  
          case 97:
          foo += 896;break;
  
          case 98:
          foo += 34;break;
  
          case 99:
          foo += 53;break;
        }
  
      }
  
  
    }
  
  } else {
  
    if(curTest < 150) {
  
      if(curTest < 125) {
  
        switch(curTest) {
          case 10:
          foo += 247;break;
  
          case 11:
          foo += 43;break;
  
          case 12:
          foo += 2;break;
  
          case 13:
          foo += 345;break;
  
          case 14:
          foo += 43;break;
  
          case 15:
          foo += 889;break;
  
          case 16:
          foo += 65;break;
  
          case 17:
          foo += 4;break;
  
          case 18:
          foo += 15;break;
  
          case 19:
          foo += 64;break;
  
          case 110:
          foo += 34;break;
  
          case 111:
          foo += 97;break;
  
          case 112:
          foo += 34;break;
  
          case 113:
          foo += 65;break;
  
          case 114:
          foo += 23;break;
  
          case 115:
          foo += 99;break;
  
          case 116:
          foo += 99;break;
  
          case 117:
          foo += 209;break;
  
          case 118:
          foo += 34;break;
  
          case 119:
          foo += 48;break;
  
          case 120:
          foo += 33;break;
  
          case 121:
          foo += 155;break;
  
          case 122:
          foo += 896;break;
  
          case 123:
          foo += 34;break;
  
          case 124:
          foo += 53;break;
  
        }
  
      } else {
  
        switch(curTest) {
          case 125:
          foo += 247;break;
  
          case 126:
          foo += 247;break;
  
        }
  
      }
  
  
    }
  
  
  }
  
  
  if(foo > 1000) {window.strthing=foo;foo=0;} /* just to make sure we don't get optimized away */
  };
  
  var doHash = function() {
     actions[curTest]();
     if(foo > 1000) {window.strthing=foo;foo=0;} /* just to make sure we don't get optimized away */
  };

Test runner

Ready to run.

Testing in
TestOps/sec
Switch
curTest ++;
if(curTest >= 127) curTest = 0;
doSwitch();
ready
Hash
curTest ++;
if(curTest >= 127) curTest = 0;
doHash();
ready
IfElse
curTest ++;
if(curTest >= 127) curTest = 0;
doIfElse();
ready

Revisions

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