Can someone optimize the following code snippet for better performance?
function whichSideOfTheBoard(name) {
const light = ['Luke', 'Obi-Wan', 'Front'];
const dark = ['Vader', 'Palpatine'];
return light.includes(name) ? 'light' :
dark.includes(name) ? 'dark' : 'unknown';
};
whichSideOfTheBoard('Front');
// returns "light"
whichSideOfTheBoard('Back');
// returns "unknown"
Define an object which has the key of 'name' and value of dark/light and simply just return obj[name] || 'unknown'
function whichSideOfTheBoard(name) {
const names = {
Luke: 'light',
'Obi-Wan': 'light',
Front: 'light',
Vader: 'dark',
Palpatine: 'dark'
};
return names[name] || 'unknown';
};