Skip to content

Instantly share code, notes, and snippets.

@airbr
Created November 14, 2021 06:09
Show Gist options
  • Save airbr/dcc7f4756b8961822bedcfb167921d0e to your computer and use it in GitHub Desktop.
Save airbr/dcc7f4756b8961822bedcfb167921d0e to your computer and use it in GitHub Desktop.
JavaScript deep object comparison
function deepEqual(a, b) {
if (a === b) return true;
if (a == null || typeof a != "object" ||
b == null || typeof b != "object") return false;
let keysA = Object.keys(a), keysB = Object.keys(b);
if (keysA.length != keysB.length) return false;
for (let key of keysA) {
if (!keysB.includes(key) || !deepEqual(a[key], b[key])) return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment