Aditia Mahdar


kodelasut

Web Developer. Railsnoob. Freelancer

Jan 27, 2018

[Javascript] The Fastest Conditionals

The three techniques presented here—the if statement, the switch statement, and array lookup—each have their uses in optimizing code execution:

Use the if statement when:

  • There are no more than two discrete values for which to test.
  • There are a large number of values that can be easily separated into ranges.


Use the switch statement when:

  • There are more than two but fewer than 10 discrete values for which to test.
  • There are no ranges for conditions because the values are nonlinear.


Use array lookup when:

  • There are more than 10 values for which to test.
  • The results of the conditions are single values rather than a number of actions to be taken.

No comments:

Post a Comment