-
Notifications
You must be signed in to change notification settings - Fork 0
deepEqual
Saurav-TB-Pandey edited this page Aug 10, 2026
·
2 revisions
A highly optimized utility function that deeply compares two values for structural equality. It handles complex nesting, circular references without crashing, and accurately compares native JavaScript objects like Date, RegExp, Map, and Set.
Comparing complex, deeply nested objects.
import { deepEqual } from "react-hook-lab";
const objA = { user: { name: "Alice", active: true } };
const objB = { user: { name: "Alice", active: true } };
console.log(objA === objB); // false (different memory references)
console.log(deepEqual(objA, objB)); // true (structurally identical!)Comparing advanced JavaScript data types.
import { deepEqual } from "react-hook-lab";
const dateA = new Date("2024-01-01");
const dateB = new Date("2024-01-01");
console.log(deepEqual(dateA, dateB)); // true
const regexA = /hello/ig;
const regexB = /hello/ig;
console.log(deepEqual(regexA, regexB)); // true
const mapA = new Map([['key', { val: 1 }]]);
const mapB = new Map([['key', { val: 1 }]]);
console.log(deepEqual(mapA, mapB)); // true-
a(any): The first value to compare. -
b(any): The second value to compare.
Returns a strict boolean. true if the values are structurally equal, false otherwise.
-
Fast-path Reference Check: It immediately returns
trueifObject.is(a, b)matches. -
Circular Reference Handling: It uses an internal
WeakMapto track objects it has already visited. If it encounters a self-referential loop, it correctly resolves the equality instead of throwing a Maximum Call Stack Size Exceeded error. -
Type-Specific Checks: It leverages instance checks (
instanceof) to apply specialized, high-performance comparisons forArray,Date,RegExp,Map, andSet. -
Object Key Iteration: For plain objects, it checks if both objects have the exact same number of keys, and then recursively checks
deepEqualon every key-value pair.
Built with ❤️ by Saurav-TB-Pandey. | Report an Issue | NPM