39

Now, before you all jump on me and say "you're over concerned about performance," let it hereby stand that I ask this more out of curiosity than rather an overzealous nature. That said...

I am curious if there is a performance difference between use of the && ("and") operator and nested if statements. Also, is there an actual processing difference? I.e., does && always process both statements, or will it stop @ the first one if the first one fails? How would that be different than nested if statements?

Examples to be clear:

A) && ("and") operator

if(a == b && c == d) { ...perform some code fashizzle... }

versus B) nested if statements

if(a == b) {
    if(c == d) { ...perform some code fashizzle... }
}
1
  • 1
    Plus one for proper grammar and spelling. Commented Apr 2, 2020 at 13:17

4 Answers 4

33

The performance difference is negligible. The && operator won't check the right hand expression when the left hand expression evaluates false. However, the & operator will check both regardless, maybe your confusion is caused by this fact.

In this particular example, I'd just choose the one using &&, since that's better readable.

2
  • 2
    Wasn't aware of forced & operator. That's interesting!
    – arxpoetica
    Commented Jun 27, 2010 at 2:56
  • 19
    Note that & is the /bitwise/ and operator. Thinking of it as a 'forced' version of && isn't quite correct. Example: 1 && 2 is 2 while 1 & 2 is 0. Commented Jun 27, 2010 at 3:10
15

If you're concerned about performance, then make sure that a==b is more likely to fail than c==d. That way the if statement will fail early.

4
  • 1
    Could someone shed some light on why I got a down vote on my answer? Commented Jun 27, 2010 at 3:20
  • I don't know -- your suggestion seems fine to me so I've upvoted you. Commented Jun 27, 2010 at 3:47
  • My guess is the -1 was because it doesn't reeaally address the question, which was a curiosity about a particular point of Javascript, rather than general optimization.
    – Matchu
    Commented Jun 27, 2010 at 3:50
  • @Warren - Thanks. :) @Matchu - Well, it was already addressed by so many others in the thread. I just wanted to give him a hint on what to think about when it comes to IF statement design and performance. But I appreciate your comment. Thanks. Commented Jun 27, 2010 at 3:55
3

Like nested ifs, && is lazy.
The expression a && b will only evaluate b if a is truthful.

Therefore, the two cases should be completely identical, in both functionality and performance.

1
  • 2
    You probably meant "will only evaluate b if a is truthful". Commented Jun 27, 2010 at 2:52
3

A peformance test might help clear things up: http://jsperf.com/simey-if-vs-if

Seems the performance difference is incredibly negligable between the two; However as @Gert mentioned, failing early really improves things.

2
  • 1
    To further this test case, I created a JSPerf (jsperf.com/operator-if-vs-nested-if) where the 2nd check is a slower performing one and the first is just a boolean. This gives a good comparison between the parser ignoring the 2nd check with the && operator and nesting the checks. In my test, using inline && is actually slightly faster than nested ifs (but only about ~0.1%)
    – Mark
    Commented Sep 1, 2018 at 0:07
  • oh nice, Mark, your test shows much clearer the benefit of failing early!
    – simey.me
    Commented Sep 4, 2018 at 1:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.