Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36
Chrome 80
Windows
Desktop
4 years ago
Test name Executions per second
Object literal 2025288.9 Ops/sec
Switch 2022408.9 Ops/sec
Script Preparation code:
AخA
 
var brands = [
"Mastercard",
"Visa",
"AmericanExpress",
"DinersClub",
"Discover",
"JCB",
"UnionPay"
]
var brand = brands[Math.floor(Math.random() * 7)];
Tests:
  • Object literal

     
    var map = {
    Mastercard:"Mastercard",
    Visa:"Visa",
    AmericanExpress:"American Express",
    DinersClub: "Diners Club",
    Discover: "Discover",
    JCB: "JCB",
    UnionPay:"UnionPay",
    }
    map[brand]
  • Switch

     
      switch (brand) {
        case "Mastercard":
          return "Mastercard";
        case "Visa":
          return "Visa";
        case "American Express":
          return "AmericanExpress";
        case "Diners Club":
          return "DinersClub";
        case "Discover":
          return "Discover";
        case "JCB":
          return "Jcb";
        case "UnionPay":
          return "UnionPay";
        default:
          return "UnknownCard";
      }