How can I determine equality for two JavaScript objects?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Object equality in JavaScript is tricky because the language distinguishes between reference identity and structural equality. Two objects can contain the same data and still compare as unequal with === because they are different objects in memory.
Reference Equality Comes First
For objects, === checks whether both variables point to the exact same object.
That behavior is correct and important. It answers "are these the same object?" not "do these objects contain the same values?"
Shallow Equality
If you only care about top-level properties, a shallow comparison is often enough.
This is useful in some UI optimization paths, but nested objects still compare by reference.
Deep Equality
When nested arrays and objects matter, you need a recursive comparison.
That gets you much closer to semantic equality, though fully correct deep equality across every JavaScript type is more involved than a short helper suggests.
Why JSON.stringify Is Not a General Solution
Many developers try this shortcut:
It can work for simple plain objects, but it has important limits:
- Property order can affect the result
- '
undefined, functions, and symbols are not represented normally' - Dates, maps, sets, and custom prototypes are not compared meaningfully
- Circular references throw errors
So JSON.stringify is fine for controlled data shapes, but it is not a robust equality strategy for general objects.
Libraries Are Often the Practical Choice
If you need production-grade deep equality, a well-tested library is often better than maintaining your own edge cases.
That is especially helpful when your data includes arrays, dates, nested structures, or values that need careful comparison semantics.
Pick the Equality Rule That Matches the Problem
The right comparison depends on what you are trying to prove:
- Reference identity for cache keys, mutation tracking, or React dependency behavior
- Shallow equality for lightweight prop checks
- Deep equality for data validation, testing, or state comparison
There is no single "object equality" rule that is correct for every application.
Common Pitfalls
The biggest mistake is expecting === to compare object contents. For objects, it only compares references.
Another issue is using JSON.stringify as a universal deep-equality check. It silently fails as a general rule once values become more complex than plain JSON-safe objects.
Developers also write quick recursive comparators and forget edge cases such as arrays, null handling, dates, or circular references.
Finally, deep equality can be expensive. If you are calling it on large objects in performance-sensitive UI code, make sure the extra work is actually justified.
Summary
- '
===checks object identity, not structural equality.' - Use shallow comparison only when top-level properties are enough.
- Use deep comparison for nested objects and arrays.
- '
JSON.stringifyworks only for limited, controlled data shapes.' - For production-grade deep equality, a tested library is often the most reliable choice.
Related reading
- How can I determine if my React Native app is a debug or release build from JavaScript code?
- How can I display full non-truncated dataframe information in HTML when converting from Pandas dataframe to HTML?
- How can I do Base64 encoding in Node.js?
- How can I efficiently parse HTML with Java?
- How can I find each table cell's visual location using jQuery?
- How can I generate an apk that can run without server with react-native?
- How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?
- How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.