Switch-case vs. if/else (v14)

Revision 14 of this benchmark created on


Setup

let v = 0 | Math.random() * 9 + 1; // 1 to 9
  let sum;
  const sumObj = {
   1: "A",
   2: "B",
   3: "C",
   4: "D",
   5: "E",
   6: "F",
   7: "G",
   8: "H",
   9: "I"
  };
  const sumObjFunct = {
   1: () => "A",
   2: () => "B",
   3: () => "C",
   4: () => "D",
   5: () => "E",
   6: () => "F",
   7: () => "G",
   8: () => "H",
   9: () => "I"
  }
  
  const sumArr = ['A','B','C','D','E','F','I','G','H','I']

Test runner

Ready to run.

Testing in
TestOps/sec
if/else strict
sum = sumObjFunct[v]() ?? sumObjFunct[9]()
ready
Array
sum = sumArr[v -1] ||  "?";
ready

Revisions

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