switch vs objects literals

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
if-else
function getValueIfElse(val) {
  if (val === 0) {
    return 0
  } else if (val === 1) {
    return 1
  } else if (val === 2) {
    return 2
  } else {
    return 10000000
  }
}
ready
switch-case
function getValueSwitch(val) {
  switch (val) {
    case 0:
      return 0
    case 1:
      return 1
    case 2:
      return 2
    default:
      return 10000000
  }
}
ready
object-literal
const values = {
  '0': 0,
  '1': 1,
  '2': 2,
}

function getValue(val) {
  return values[val] || 10000000
}
ready

Revisions

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