Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Web
Expertise: Beginner
Nov 18, 1999

Arrange Conditions Properly

You can optimize the performance of your "JavaScript" code by properly arranging the conditions in the "if" statement. If you have several conditions to be tested together, and you know that one is more likely to pass or fail than any of the others, depending on whether the tests are connected with OR (||) or AND (&&), you can speed execution of your script by putting that condition first in the conditional statement.

For example, if three conditions must all be true (using && operators) and the second test fails, the third condition is not tested. Similarly, if only one of several conditions must be true (using || operators), testing stops as soon as any one condition passes the test. This is particularly effective if the conditions to be tested involve execution of function calls or other code.

In this example, the second part of the condition will not be evaluated if the first part of condition returns true. Notice that in this case it will save you a function call.

 
if ((firstfunction() == true) || (secondfunction() === true))
{
    // code
}
else
{
    // code
}
Deepak Pant
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date