1

Possible Duplicate:
JavaScript === vs == : Does it matter which “equal” operator I use?

I always assumed that == is faster than === operator. But after some reading I am confused. Is there any benefit at performance level if I use === over == operator?

Community
  • 1
  • 1
Anoop
  • 23,044
  • 10
  • 62
  • 76
  • 1
    Don't bother with which one is faster; use whichever one is right for any given bit of code. Trying to optimise at this level is like sweeping a bit of the snow off the Titanic's iceberg - it'll make you feel heroic, but it won't actually make any noticeable difference. – Spudley Sep 08 '12 at 17:37
  • This is what http://jsperf.com/ was built for! – iConnor Aug 18 '13 at 04:16

3 Answers3

10

The === operator will be faster than the == operator. This is because === doesn't need to check multiple comparisons, while == does (i.e., == performs conversions).

return "true" == true; //true

The above will first test to see if "true" === true which is false, then check "true" === "true" (i.e., it converts the bool to a string, then checks again).

Read the comments below. You can also look at these two benchmarks as sort of guides:

For future reference, though, they really aren't the same thing and you shouldn't use them interchangeably. In fact, there aren't many any cases you'd really want to use == over ===. It'll usually lead to unexpected or seemingly random results while the main point of programming is to create an interface your user can travel on. With conditions that don't always evaluate to what you originally test for, programs can turn out to be buggy, messy, or unreliable.

jeremy
  • 9,965
  • 4
  • 39
  • 59
  • 3
    It isn't necessarily true that it "will be faster" and depends upon implementation. The apparent increase in complexity does not necessarily translate to real-world performance decreases. (Ideally both should be considered "the same speed" and the appropriate equality operator should be used.) –  Sep 08 '12 at 17:22
  • @pst How should I phrase it? In certain cases, it can be faster as no conversions take place? – jeremy Sep 08 '12 at 17:23
  • 1
    I would have just not mentioned one "being faster"; the importance is, as pointed out later, the semantics. Also, in all cases where `x == y` and `x === y`, I *believe* they may only evaluate the *same* sequence of operations. –  Sep 08 '12 at 17:27
  • Nile: When the "type" of the operands are the same, `==` and `===` are identical. But when the types are different, the `==` will have more work to do. But as others pointed out, speed is irrelevant. What matters is the proper behaviour for the task at hand. – gray state is coming Sep 08 '12 at 17:43
  • @graystateiscoming I agree with the latter sentence, but === appears to be faster when the typeof are the same. http://jsperf.com/2-vs-3-eq-2 – jeremy Sep 08 '12 at 17:49
  • Nile: It comes down to the implementation, but if you read the specification, the algorithm followed by `===` and `==` is identical when the types match. They both start with a `Type()` test of both operands, and if the types are the same, they both do the same work. If anything, I'd be a little sceptical of the testing environment. – gray state is coming Sep 08 '12 at 17:53
2

While the === operator might perform faster than ==, it's really hard to distinguish the speed difference in most cases so you can freely use whichever of those two options makes your code clearer.

Luka
  • 1,718
  • 3
  • 19
  • 29
0

You can try with === operator is faster

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51