Switch case, if else or a lookup map — A study case

Maya Shavin
Dec 11, 2018 · 7 min read
Image for post
Image for post
Credit by VectorStock — Select clothes based on mood

The case — Dress up according to my mood

Image for post
Image for post
dressUp() — to help you decide what to wear without thinking

If my mood is <happy> then give me <red dress>

Image for post
Image for post
if (mood === 'happy') // Do something
else if (mood === 'excited') // Do something
else if (mood === 'slightly optimistic') // Do something
else if (mood === 'optimistic') // Do something
else if (mood === 'gloomy') // Do something
// ...And the show continues

Switch my clothes in case I don’t feel great

Image for post
Image for post
A sample flow diagram of switch
Image for post
Image for post
case 'happy':
case 'slightly happy':
//Code
break;

Map my mood and return the correct clothes

Image for post
Image for post

A JsPerf test

Image for post
Image for post
Chrome vs Firefox vs Safari

Conclusion


Frontend Weekly

It's really hard to keep up with all the front-end…

Maya Shavin

Written by

Senior Front-End Developer @Cloudinary www.mayashavin.com

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

Maya Shavin

Written by

Senior Front-End Developer @Cloudinary www.mayashavin.com

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store