0

This is not a question regarding which one should I use, but a rather a question about using the '==' operator.

Lets say I have this:

let stringNumber = '2';
let numberNumber = 2;

If I do this comparison:

stringNumber == numberNumber

Result is of course true. The '==' is doing type conversion, and that leads to two questions.

  • How big is the performance impact, when the '==' makes type conversion?
  • Is the '===' doing any type of conversion at all, or just 'does it'?
Amiga500
  • 5,330
  • 9
  • 56
  • 103
  • Please read my question again, before marking it as a duplicate – Amiga500 Dec 03 '18 at 08:37
  • 1
    Well there are performance measures in the answers to that question... – Nick stands with Ukraine Dec 03 '18 at 08:37
  • The answer to your second question is "just does it". – Barmar Dec 03 '18 at 08:38
  • 5
    In general: if you're worried about performance, measure it. You should not be worried about performance unless you have proven that you need to. You will probably never have to worry about this performance difference because it's bound to be extremely minimal. You should only be concerned with which version does the correct thing, not which is more performant at the cost of doing the wrong thing. – deceze Dec 03 '18 at 08:39
  • 2
    "How big is the performance impact" really depends on your needs and how exactly the code is used. – haim770 Dec 03 '18 at 08:39
  • 3
    [Premature optimization is the root of all evil](http://c2.com/cgi/wiki?PrematureOptimization) – Barmar Dec 03 '18 at 08:39
  • 1
    Hi, the `==` is slightly slower than `===`, because it involves more operations as you can read [here](https://www.ecma-international.org/ecma-262/#sec-equality-operators-runtime-semantics-evaluation). But the reason why you should use `===` in place of `==` isn't a matter of speed, but a matter of bug prone programming. A couple of comparison less won't make your code faster! – Cristian Traìna Dec 03 '18 at 08:41
  • "*How big is the performance impact, when the '==' makes type conversion?*" - as big as when you do `stringNumber === String(numberNumber)`. "*Is the '===' doing any type of conversion at all?*" - no, it only does type checks and when they don't match it returns `false`. – Bergi Dec 03 '18 at 09:43

0 Answers0