Compare two objects' properties to find differences?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Comparing two objects to find property differences is common in change tracking, audit logging, unit testing, and data synchronization. The approach depends on your language: JavaScript uses Object.keys with iteration, Python uses __dict__ comparison or the deepdiff library, C# uses reflection, and Java uses reflection or EqualsBuilder. Each method detects which properties changed, what the old and new values are, and whether properties were added or removed.
JavaScript
This shallow comparison works for primitive values. For nested objects, use deep comparison.
JavaScript Deep Comparison
Python
The deepdiff library handles nested objects, lists, sets, and type changes automatically.
C# with Reflection
Java with Reflection
Using Libraries
Common Pitfalls
- Shallow comparison on nested objects: Using
===or==on objects compares references, not contents. Two objects with identical properties are not equal by reference. Always recurse into nested objects or use deep comparison libraries. - Missing keys treated as undefined vs absent: In JavaScript,
obj.missingreturnsundefined, which may falsely match a property explicitly set toundefined. UsehasOwnPropertyto distinguish missing from undefined. - Reflection performance in hot paths: Reflection-based comparison in C# and Java is slow compared to manual property checks. Cache
PropertyInfo/Fieldarrays and avoid reflection in loops processing thousands of objects. - Circular references causing infinite recursion: Objects that reference each other (A.parent = B, B.child = A) cause stack overflow in recursive comparison. Track visited objects in a Set or use libraries with built-in circular reference detection.
- Ignoring type coercion: In JavaScript,
1 == "1"istruewith loose equality. Always use strict equality (===) when comparing property values to avoid false "no difference" results.
Summary
- JavaScript: iterate
Object.keyswith===for shallow, recurse for deep, or use Lodash_.isEqual - Python: compare
__dict__for simple objects, usedeepdifffor nested structures - C#: use
GetProperties()reflection to compare all public properties - Java: use
getDeclaredFields()reflection withObjects.equals()for null safety - For production use, prefer tested libraries (deepdiff, Lodash, Apache Commons) over hand-rolled comparison
Related reading
- Comparing scikit learn clusterings using a decision tree
- Comparing two NumPy arrays for equality, element-wise
- Comparison-based ranking algorithm
- Complex dataset split - StratifiedGroupShuffleSplit
- Compute rolling maximum drawdown of pandas Series
- Computing a moving maximum
- Concatenate a list of pandas dataframes together
- Concatenate a NumPy array to another NumPy array
.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.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.