MCPcopy Create free account
hub / github.com/marijnh/Eloquent-JavaScript / deepEqual

Function deepEqual

code/solutions/04_4_deep_comparison.js:1–16  ·  view source on GitHub ↗
(a, b)

Source from the content-addressed store, hash-verified

1function deepEqual(a, b) {
2 if (a === b) return true;
3
4 if (a == null || typeof a != "object" ||
5 b == null || typeof b != "object") return false;
6
7 let keysA = Object.keys(a), keysB = Object.keys(b);
8
9 if (keysA.length != keysB.length) return false;
10
11 for (let key of keysA) {
12 if (!keysB.includes(key) || !deepEqual(a[key], b[key])) return false;
13 }
14
15 return true;
16}
17
18let obj = {here: {is: "an"}, object: 2};
19console.log(deepEqual(obj, obj));

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected