How to DEEP COMPARE JavaScript objects?
This code helps to deep compare objects containing string, number, boolean, function, array, and nested objects.
The fastest way to compare two objects is to use JSON parsing methods, unless the ordering in both objects are different.
Then you need to hard code by using for-in loop.
The idea is to use JSON-first methods:
-
Firstly compare object size by Object.keys().length and/or Array.length
-
Secondly compare objects by JSON.parse()
-
thirdly compare objects based on data type