A while back in college, I read somewhere that when there are a similar number of conditions in an if-else block and a switch statement, the switch statement is faster.
Recently, during a FreeCodeCamp JavaScript section project, I had to use a bunch of conditions (I know it could have been done in a better way) in multiple functions. I decided to try using switch statements since that would apparently be faster. But as soon as I wrote the first function, I realized that although it would be faster, it looked ugly. I used good old if-else blocks in the other functions.
And although I had to think quite a bit about the order of the conditions, it looked a bit cleaner than the former.
For me, I would use if-else blocks instead of switch statements until it's absolutely necessary. This was btw my first use of switch statements in around 2 years since last I used them in college.
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.
A while back in college, I read somewhere that when there are a similar number of conditions in an
if-elseblock and aswitchstatement, theswitchstatement is faster.Recently, during a FreeCodeCamp JavaScript section project, I had to use a bunch of conditions (I know it could have been done in a better way) in multiple functions. I decided to try using
switchstatements since that would apparently be faster. But as soon as I wrote the first function, I realized that although it would be faster, it looked ugly. I used good oldif-elseblocks in the other functions.And although I had to think quite a bit about the order of the conditions, it looked a bit cleaner than the former.
For me, I would use
if-elseblocks instead ofswitchstatements until it's absolutely necessary. This was btw my first use ofswitchstatements in around 2 years since last I used them in college.