DEV Community

loading...

Discussion on: 5 ways to refactor if/else statements in JS functions

Collapse
afpaiva profile image
Andre Paiva

Hi Sylwia. Great article, congrats!

Let me do some noob questions?
Using [switch] in a situation with many [if/else if], will offer a better performance?
And is there difference by using [switch] or [if] or vice versa on a compiled or non compiled programming language? Like, on C it would be better this, and on JS better that?

Thanks a lot!

Collapse
sylwiavargas profile image
Sylwia Vargas Author

Hi @afpaiva ! Thank you for your question!
In comparison with if/else statements, switch statements are a bit faster with small number of cases and get incrementally faster with new conditions. Readability, it seems that devs prefer to read if/else for two conditions and then switches for more.
There's this great table from O'reilly:
performance table comparing switches, if/else and table lookups

Talking about performance in C is both beyond my expertise and comfort level, sadly!