-2

I'm just curious.. how does the performance differ between the different versions of conditional statements?

Namely:

if ( x == false) //doSomething

if ( x === false ) //doSomething

if ( !x ) //doSomething

!x && //doSomething

Personally, I would prefer using the 3rd one since it is a lot shorter than the other three. But I am a bit skeptic to whether I am unaware of a "possible" performance drawback. Even if it is just a difference of milliseconds, it matters for me.

Thanks!

AJ Naidas
  • 1,424
  • 7
  • 25
  • 47
  • 1
    You are aware that there are functional differences, yes? – David Hedlund Apr 29 '13 at 06:37
  • http://stackoverflow.com/questions/523643/difference-between-and-in-javascript and http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use – Prasath K Apr 29 '13 at 06:38
  • "how does the performance differ" — In which version of which JavaScript runtime? These things change as different optimisations are added. – Quentin Apr 29 '13 at 06:40
  • @PrasathK wow thanks! I never knew about the difference of == and ===. Well anyway, I think the question is still answerable since i'm looking for performance difference. Thanks for the link! – AJ Naidas Apr 29 '13 at 06:41
  • @AJNaidas it's ok ... – Prasath K Apr 29 '13 at 07:43
  • downvotes even though I got to learn something and a particular friend learned something from this post as well. Thanks @Ramunas for the performance-related answer – AJ Naidas May 02 '13 at 07:07

2 Answers2

3

I will not dive into functional differences between statements as Ted already did this but you can always check your JS performance at http://www.jsperf.com

Here is yours: http://jsperf.com/ifperf

Ramunas
  • 3,853
  • 1
  • 30
  • 31
  • 1
    That only tests the performance when `x` is `false`. A better test would include the effects of `x` being other values (including `undefined` and `NaN`). Also, I've found jsperf to be unreliable for such microbenchmarking. – Ted Hopp Apr 29 '13 at 06:50
  • Yeah, sure. No offence to OP, but I think he did not know of such site/web tool and therefore asked a question here. Now he can test whatever he wants to: `undefined`, `NaN`, `0`, `1`, `{}`, `null` etc. And a question was about performance – Ramunas Apr 29 '13 at 06:54
  • Yep. It would also be better to initialize `x` in the setup code, rather than timing both the assignment and the `if` test. – Ted Hopp Apr 29 '13 at 06:55
2

These four variations have different semantics and/or syntactic constraints.

if ( x == false) //doSomething
if ( !x ) //doSomething

These two are equivalent and will doSomething if x is any "falsy" value — false, 0, undefined, null, NaN, or the empty string. I doubt that after parsing there will be any performance difference at all.

if ( x === false) //doSomething

This will doSomething only if x is exactly false. If x is any other "falsy" value (or any "truthy" value) then doSomething will not execute. There may be a slight performance gain because no type conversion takes place.

!x && //doSomething

This is more or less equivalent to the first two, but doSomething is restricted to be an expression. I would expect performance to be like the first two.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521