2

I follow the practice of always using a absolute comparison '===', but some learned colleagues have argued that this is an unnecessary waste of computation time on type-checking in situations where you're highly confident of a particular comparison type.

So my question is this; is there realistically any significant performance hit for always type-checking values during comparison?

monners
  • 5,174
  • 2
  • 28
  • 47
  • 3
    Did you try measuring the performance difference ? – Denys Séguret Oct 10 '13 at 08:30
  • 2
    `==` coerces values to perform the check, so an argument can be made in favor of `===` as well. – holographic-principle Oct 10 '13 at 08:32
  • Look at [Difference between == and === in JavaScript](http://stackoverflow.com/questions/523643/difference-between-and-in-javascript) – Simon Dorociak Oct 10 '13 at 08:32
  • 3
    " ... **significant** performance difference..." Most likely not. But try yourself: http://jsperf.com/. – Felix Kling Oct 10 '13 at 08:33
  • The term "significant performance difference" makes no sense outside of the context of specific code. Most likely the equality operator you use will not be the performance bottleneck but it really depends. – Benjamin Gruenbaum Oct 10 '13 at 08:33
  • @BenjaminGruenbaum this question doesn't seem to have proper answers on the performance topic. On this, I know I did measurements a long time ago and was disappointed to be unable to see anything. So on the "significant" thing, I can only agree with Felix. – Denys Séguret Oct 10 '13 at 08:33
  • [no significative (nor reliably reproductible) difference](http://jsperf.com/equalitydoubletriple/5). – Denys Séguret Oct 10 '13 at 08:37
  • @dystroy even if you don't like the _answers_ it's the same question, and I quote: `Is there a performance benefit to replacing == with ===?` – Benjamin Gruenbaum Oct 10 '13 at 08:39
  • @BenjaminGruenbaum There's no point, in my opinion, in linking to a more general question which has no answer at all on the specific question asked by monners. – Denys Séguret Oct 10 '13 at 08:41

3 Answers3

4

There's probably not a significant performance difference if the objects are already of the same type. Both operators have to look up and compare the types of the two arguments before doing the data comparison.

If the objects are of different types, === should be a little faster, since it can return false immediately, while == will coerce one of the arguments to the other type and then perform the comparison.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

There's a test of this at jsperf:

Take a look and try it yourself: http://jsperf.com/equalitydoubletriple/4

Edit: This test may be better, as @dystroy mentions in the comment below: http://jsperf.com/equalitydoubletriple/5

becquerel
  • 1,131
  • 7
  • 11
1

There's no significant performance difference between == and ===. The only difference between them is the type comparison. == comparison can be used even when the 2 conditions have different type. === comparison only accept comparison between 2 same type conditions. Example: when int and char can be compared with ==, === comparison can't be used.

david
  • 3,225
  • 9
  • 30
  • 43