getDiscount() perf

Benchmark created on


Setup

function getDiscountA(category) {
  let discount;
  
  if (category === "student") {
    discount = 20;
  } else if (category === "senior") {
    discount = 30;
  } else if (category === "military") {
    discount = 25;
  } else {
    discount = 0;
  }
  
  return discount;
}

function getDiscountAEaryReturn(category) {
  if (category === "student") {
    return 20;
  } else if (category === "senior") {
    return  30;
  } else if (category === "military") {
    return  25;
  }
  
  return 0;
}

function getDiscountB(category) {
  return {
    "student": 20,
    "senior": 30,
    "military": 25
  }[category] || 0;
}

const discounts = {
  student: 20,
  senior: 30,
  military: 25
};

function getDiscountC(category) {
  return discounts[category] ?? 0;
}

const discountsMap = new Map([
  ["student", 20],
  ["senior", 30],
  ["military", 25],
]);

function getDiscountCMapped(category) {
  return discountsMap.get(category) ?? 0;
}

Test runner

Ready to run.

Testing in
TestOps/sec
getDiscountA()
getDiscountA("student");
getDiscountA("senior");
getDiscountA("military");
getDiscountA("unknown");
ready
getDiscountAEaryReturn()
getDiscountAEaryReturn("student");
getDiscountAEaryReturn("senior");
getDiscountAEaryReturn("military");
getDiscountAEaryReturn("unknown");
ready
getDiscountB()
getDiscountB("student");
getDiscountB("senior");
getDiscountB("military");
getDiscountB("unknown");
ready
getDiscountC()
getDiscountC("student");
getDiscountC("senior");
getDiscountC("military");
getDiscountC("unknown");
ready
getDiscountCMapped()
getDiscountCMapped("student");
getDiscountCMapped("senior");
getDiscountCMapped("military");
getDiscountCMapped("unknown");
ready

Revisions

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