DEV Community

Discussion on: 8 Best JavaScript One-Liners

Collapse
 
skyjur profile image
Ski • Edited

Please never do #4: comparing using JSON.stringify(a) == JSON.stringify(b). It's very poor practice: it doesn't guarantee correctness - in some cases it can give false positive as well as false negative (for instance objects equal but key order is not JSON stringify will produce different output) - as some objects might not get stringified at all, and some values can even result into json.stringify throwing an error.

If you have class objects, then good idea is to implement equal() method on your object and compare a.equal(b). For generic object use recursive algorithm.

But more importantly consider why do you need deep equality check in first place - as it can be computationally intensive. Likely there should be ways to avoid it in first place.

Collapse
 
shivams1007 profile image
Shivam Singh

Yes it doesn't give a correct solution for every scenario