If-Else vs If-If vs Switch Javascript Performance

View complete post of If-Else vs If-If vs Switch Javascript Performance

There are three major conditional syntax that used widely in almost all programming language: If, If-else, Switch. Now then which type of conditional is going to give fastest execution time so our software may perform better even it’s just micro optimization.

Of course many of us will said that there is no chance that “if if” or “multiple if” is going to be fastest type. So which is fastest? Switch or If Else…

Now in our internet there is a lot of interesting tools to help us prove which is better and faster. Lets say for Javascript language there are jsPerf.com to do benchmarking several codes.

To be quick, see the test result from my jsPerf testcase which modified from Adrian Sanoguel original test.

jsPerf.com Testcase Result

Now we see weird result where in the “WebKit”-based browsers like Google Chrome or Safari, the performance of “multiple ifs” is far better than “if-else” and surprisingly the performance of “switch” is far behind the other methods.

While in the another browser, it make sense that “if-else” and “switch” is in par performance when “multiple ifs” is behind them. How can this happened? Maybe someone with better knowledge of Webkit can share the reason….

Leave a Reply

Your email address will not be published. Required fields are marked *

Post Navigation