Test name | Executions per second |
---|---|
Switch | 16648.4 Ops/sec |
Object Literal | 15095.6 Ops/sec |
var today = new Date().getDay()
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}`);
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]}`
);