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?
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:
Talking about performance in C is both beyond my expertise and comfort level, sadly!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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!
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 readif/else
for two conditions and thenswitch
es for more.There's this great table from O'reilly:
Talking about performance in C is both beyond my expertise and comfort level, sadly!