javascript switch() or if()

前端 未结 6 1367
孤独总比滥情好
孤独总比滥情好 2020-12-11 11:37

which would be better if i do this:

if(message == \'redirect\')
{
    is_valid.accepted = true;
}
else if(message == \'invalid id\')
{
    is_valid.accepted          


        
6条回答
  •  渐次进展
    2020-12-11 12:00

    It depends on your definition of better. Do you want it to be a better reading experience or better performance?

    I always jsPerf things. I don't really care much about readability if it makes my code faster/proper.

    Here is a jsPerf of a bunch of different switch vs. if/else if/if ==/if === statements.

    http://jsperf.com/switch-if-else/16

    This is revision 16 of the test. So if you are looking at this 10 weeks from now make sure you scroll to the bottom and grab the most recent test.

提交回复
热议问题