Possible Duplicate:
Javascript switch vs. if…else if…else
just curious if things will run faster or be laid out in cache nicer or something that might increase performance by using a switch? At very least I know it looks nice and allows the next code to see that all the next sequential statements are dependent upon the evaluation of the same variable.
In general,
switchis faster thanif - else ifstatements.However, kind of best practice is to use
if - else ifif you got max 3 conditionals. If you’re going beyond that, you should useswitchstatements.The problem with
if elseis that it possibly needs to check multiple times before it finally reaches the code to execute. Therefore you also need to optimize the order for your conditional statements.That code would not make much sense from a performance prospective if you expect
bazto betrueandfoo/barto befalsemost of the times.