r/learnjavascript • • May 06 '21

JS: Multiple Condition Checking

Post image
484 Upvotes

61 comments sorted by

View all comments

56

u/dethstrobe May 06 '21

A quick performance check. https://codesandbox.io/s/awesome-jones-2fpy1?file=/src/index.js

long hand:  0.12499996228143573
short hand 1:  0.7600000244565308
short hand 2:  0.1700000138953328

Long hand is faster because you don't need to waste time and memory space building an array. Premature optimization is the root of all evil. Just keep things simple.

1

u/[deleted] May 10 '21 edited May 10 '21

I can make it 0.09 if it does matter for you.

https://codesandbox.io/s/competent-visvesvaraya-qqci5?file=/src/index.js:418-573

just make a dictionary and get your value. what's the point? well in this situation nothing besides being faster. in a particular situation? avoiding checking every single condition if you already know what you are checking for. Every book should get rid of switch condition, and you should avoid that much if statements in your methods. imho