Run details:
Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0
Firefox 119
Linux
Desktop
one year ago
Test name Executions per second
map lookup 352233408.0 Ops/sec
switch case 462321536.0 Ops/sec
if/else 45071364.0 Ops/sec
Script Preparation code:
AخA
 
var map = new Map([
  [0, "Initializing"],
  [1, "Idle"],
  [2, "Running"],
  [3, "Stop"],
  [4, "Error"],
]);
function switchCase(value) {
  switch (value) {
    case 0: return "Initializing";
    case 1: return "Idle";
    case 2: return "Running";
    case 3: return "Stop";
    case 4: return "Error";
    default: return void 0;
  }
}
function ifElse(value) {
  if (value === 0) {
    return "Initializing";
  } else if (value === 1) {
    return "Idle";
  } else if (value === 2) {
    return "Running";
  } else if (value === 3) {
    return "Stop";
  } else if (value === 4) {
    return "Error";
  }
}
Tests:
  • map lookup

     
    let value = map.get(0);
    value = map.get(32);
    value = map.get(1);
    value = map.get(33);
    value = map.get(2);
    value = map.get(34);
    value = map.get(3);
    value = map.get(35);
    value = map.get(4);
  • switch case

     
    let value = switchCase(0);
    value = switchCase(32);
    value = switchCase(1);
    value = switchCase(33);
    value = switchCase(2);
    value = switchCase(34);
    value = switchCase(3);
    value = switchCase(35);
    value = switchCase(4);
  • if/else

     
    let value = ifElse(0);
    value = ifElse(32);
    value = ifElse(1);
    value = ifElse(33);
    value = ifElse(2);
    value = ifElse(34);
    value = ifElse(3);
    value = ifElse(35);
    value = ifElse(4);