JS: Loose vs Strict Equality Performance (strings and numbers)

Novokhatskyi Oleksii
2 min readMar 24, 2020

Epigraph

Limitation in the possibility of an enjoyment raises the value of the enjoyment. ― Sigmund Freud

Should I?..

Should I use strict equality in JS everywhere or there are some exceptions?
Maybe loose equality has better performance in some cases?
Let’s check the example with string and numbers and measure the results

Implementation: Step 1 (tools)

The only tool to use here in microseconds module (just for convenience)
https://www.npmjs.com/package/microseconds

Implementation: Step 2 (results)

Let’s create an array with strings [‘1’, ‘2’, ‘3’ …]

Let’s measure the time spent on checking the equesion (strictly)

And same but loosely

Also let’s run this for 100 times to get more results for analysis

Implementation: Step 3 (analysis)

Now we have 200 results (100 strict and 100 loose). So… who is a winner? Let’s implement some additional functions for accuracy

Trim values ​​that fall outside the selection boundaries

Calculate average

Calculate standard deviation

Finally

number of iterations: 1000000
Loose: 52872.00 +- 447.05 microseconds
Strict: 49737.85 +- 418.05 microseconds

Conclusion

As for me difference is not huge or important but it’s another reason to use strict equality or more correctly in this case ― no reason to use loose equality

For more info and to have possibility to check results by running scripts by yourself ― please, visit github repository.

Thanks for reading.
Feel free to comment, hope we’ll have a good discussion.
Also you are able to run this script by yourself and get own results, will be cool to compare different results

--

--

Novokhatskyi Oleksii
Novokhatskyi Oleksii

Written by Novokhatskyi Oleksii

Software Engineer, Co-Founder of Purport (purportapp.com), Tech Read channel (t.me/technicalread)

No responses yet