A few weeks ago, I have read this thread Is < faster than <=? about comparison operators in C
. It was said that there is no difference in the performance between <
and <=
as they are interpreted as same/similar machine commands.
At the same time, in our company's "best practices", it was said that we should always use "===" to compare things instead of "==". So, I started to wonder if this is always appropriate as I am used to using the "==" and "typeof ... == " and do not want to change my way of writing :-]
Note that this is in the context of JavaScript.
So, I have a little research and here Which equals operator (== vs ===) should be used in JavaScript comparisons? it is said that:
This is because the equality operator == does type coercion...meaning that the interpreter implicitly tries to convert the values and then does the comparing.
On the other hand, the identity operator === does not do type coercion, and so thus it does not convert the values of the values when comparing
And I started to wonder if this means that when I use the "===" operator, I will get good performance as no resources will be spent on converting the operands. And after all code is turned into machine commands, does this mean that just as there is no difference in C
when you use <
and <=
, this is the same in JavaScript and other languages?
===
. I don't see a choice here. – Scopas===
is faster than==
in V8, when the compiler can prove the types are the same by performing analysis - subsequent runs of the code can shortcut in===
that they cannot in==
. This is implementation detail and might change - use whichever operator is correct. – Wolffisha
or not - generally=== false
is faster but it's important to avoid premature optimizations - they also check for fundamentally different things. – Wolffish