-1

If it checks each character of a string, it can break the loop before. But I don't know how it works internally.

Example :

if(stringA === stringB)

or

if(!(stringA !== stringB))

Which is the most faster ?

  • 1
    it depends ... please add some more information, for example, the actual situation of the check. – Nina Scholz Dec 19 '18 at 10:15
  • Possible duplicate of [How do you performance test JavaScript code?](https://stackoverflow.com/questions/111368/how-do-you-performance-test-javascript-code) – Andrew Morton Dec 19 '18 at 10:18
  • 1
    Possible duplicate of [JavaScript - === vs == operators performance](https://stackoverflow.com/questions/12374815/javascript-vs-operators-performance) Using strict equality `===` over `==` is generally recommended (see this [list of JS best practices](https://medium.com/emblatech/javascript-concepts-best-practices-1f2295a2efbc)) – FK82 Dec 19 '18 at 10:18
  • 1
    `!== ` is faster than `===` if compared values have different type, in other cases they have same speed, because the is first type check then value check – ponury-kostek Dec 19 '18 at 10:18
  • 2
    Use the operator which better represents your logic, performance is not an issue in either case. – Teemu Dec 19 '18 at 10:28
  • note that string comparison especialy can use instance checking as well if the strings are constant. JS engine compiles the strings used in the program in a table of constant strings and checks if thry have the same reference. So this is extremely fast instead of char-by-char matching. Of course if a string is dynamic a char-by-char matchign is used – Nikos M. Dec 19 '18 at 10:43
  • 2
    Use the simplest one. You definitely don't want to make your code hard to read. You will care about performance when you really need it. – TGrif Dec 19 '18 at 10:49
  • If there is any difference, there will never be a real, practical situation where the difference would be relevant. – JJJ Dec 19 '18 at 12:58

1 Answers1

-3

Here I did some simulations for you to see for yourself when which operation is faster than the other one. Feel free to play around with it.

// ===
console.time();
console.log("1 === 1", 1 === 1);
console.timeEnd();

console.time();
console.log("1 === '1'", 1 === '1');
console.timeEnd();

// !==
console.time();
console.log("1 !== 1", 1 !== 1);
console.timeEnd();

console.time();
console.log("1 !== '1'", 1 !== '1');
console.timeEnd();

To sum it up roughly, !== is faster than ===.

holydragon
  • 6,158
  • 6
  • 39
  • 62
  • 2
    That is nowhere near sufficient as a conclusive test. You'll need to take each test hundreds or thousands of times and use the average to compensate for variability in how modern multi-tasking engines will schedule this on the CPU. – deceze Dec 19 '18 at 10:44
  • 1
    The real conclusion you can draw from this is that the first test is always the slowest, regardless of which one is first (you can see it yourself by re-arranging the tests). It makes sense since the JS engine can optimize further similar operations in the same script. – JJJ Dec 19 '18 at 12:57