Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36
Chrome 87
Mac OS X 10.15.6
Desktop
4 years ago
Test name Executions per second
Switch 34235.9 Ops/sec
Object Literal 34431.3 Ops/sec
Script Preparation code:
AخA
 
var today = new Date().getDay()
Tests:
  • Switch

    x
     
    let day;
    switch (today) {
      case 0:
        day = "Sunday";
        break;
      case 1:
        day = "Monday";
        break;
      case 2:
        day = "Tuesday";
        break;
      case 3:
        day = "Wednesday";
        break;
      case 4:
        day = "Thursday";
        break;
      case 5:
        day = "Friday";
        break;
      case 6:
        day = "Saturday";
        break;
      default:
        throw new Error("Hey! We have only 7 days in the week");
    }
    console.log(`What day of the week is today? ${day}`);
  • Object Literal

     
    const dayOfTheWeek = {
      0: "Sunday",
      1: "Monday",
      2: "Tuesday",
      3: "Wednesday",
      4: "Thursday",
      5: "Friday",
      6: "Saturday",
    };
    console.log(
      `What day of the week is today? ${dayOfTheWeek[today]}`
    );