+ 2

Switch v Else if

How is it that a Switch is 'faster' than an else if? It seems to me that if it is just typing, the difference is negligible at best.

10th Jul 2017, 4:48 PM
Tillman Doty
Tillman Doty - avatar
4 Answers
+ 7
Tillman Doty, you are right, the Switch statement is just faster to type while the Else If statement has more options.
10th Jul 2017, 4:59 PM
Freetoast
Freetoast - avatar
+ 3
@Tilman, neither if nor switch performance really matter, except you pass a complex expression to evaluate on each case. imo the statements executed inside a matching condition block matters more, how complex, or how computationally heavy they are. Where it's possible I would use switch because I found it be more readable. I use "if" when I need more option for condition evaluation.
12th Jul 2017, 4:11 AM
Ipang
+ 1
It's more readable and easier to type, so in return it's technically faster. As far as if it executes faster or not, honestly, I've no idea? I've never tested it, but I'll do that shortly and let you know if there is any performance gain beyond the readability/typing side of it.
10th Jul 2017, 5:05 PM
AgentSmith