Ad
  • Default User Avatar

    In High Performance JavaScript, Nicholas Zakas talks about conditionals vs. if-else statements:

    As it turns out, the switch statement is faster in most cases when compared to if-else, but significantly faster only when the number of conditions is large. The primary difference in performance between the two is that the incremental cost of an additional condition is larger for if-else than it is for switch. Therefore, our natural inclination to use if-else for a small number of conditions and a switch statement for a larger number of conditions is exactly the right advice when considering performance.

    Generally speaking, if-else is best used when there are two discrete values or a few different ranges of values for which to test. When there are more than two discrete values for which to test, the switch statement is the most optimal choice.

    https://www.safaribooksonline.com/library/view/high-performance-javascript/9781449382308/ch04s02.html