Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53
Chrome 95
Windows
Desktop
3 years ago
Test name Executions per second
Switch 82795.8 Ops/sec
Object Literal 85060.4 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]}`
    );