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.

21

u/inabahare May 06 '21

A few things to keep in mind

  1. 0.12 and 0.17 are so close that you only have things shorthand 2. It will semantically make more sense.

  2. With the above in mind, you're doing your comparison 2k times. Like you're sacrificing readability and scalability for 0.05? Like that's what they mean by premature optimizaiton being the root of all evil

  3. Those are milliseconds. Like 2k runs in 0.17 milliseconds.. Like that's not a lot of time at all

1

u/RoguePlanet1 May 07 '21

Veering a bit off-topic: I'm now wondering what the code would be to eliminate the "Likes" if they're the first word of the sentence. I should brush up on my Regex probably.

That aside, you make good points.

2

u/inabahare May 07 '21

Surprisingly not so hard, though you do end up loosing a lot of the meaning of the sentence :v

1

u/RoguePlanet1 May 09 '21

Thank you!! That's beautiful. "const likeRemover" I love it! :-p