When using a switch() statement, you add break; in between separate case: declarations. But what about the last one?
Normally I just leave it off, but I’m wondering if this has some performance implication I’m not thinking about?
I’ve been wondering about this for a while and don’t see it asked elsewhere on Stack-O, but sorry if I missed it.
I’m mainly asking this question regarding Javascript, although I’m guessing the answer will apply to all switch() statements.
Performance should not be a consideration at all. Even if some language or implementation has a performance difference that’s measurable in a micro-benchmark, it would never cause a real-world performance issue.
I think the most important issue is consistency and maintainability. If you leave off the last
breakand then another developer adds another case statement without realizing you left off thebreak, the behavior would change. It’s really the second developer’s fault for not noticing thebreakis missing, but it could have been avoided by being consistent and having thebreakon allcaseclauses.