Skip to content

Instantly share code, notes, and snippets.

@blarfoon
Last active February 21, 2022 22:52
Show Gist options
  • Save blarfoon/4ea7c0f4382c005a3a15850bf96f9e45 to your computer and use it in GitHub Desktop.
Save blarfoon/4ea7c0f4382c005a3a15850bf96f9e45 to your computer and use it in GitHub Desktop.
function areEqualDeep(a, b) {
if (typeof a == "object" && a != null && typeof b == "object" && b != null) {
const count = [0, 0];
for (const key in a) count[0]++;
for (const key in b) count[1]++;
if (count[0] - count[1] != 0) {
return false;
}
for (const key in a) {
if (!(key in b) || !areEqualDeep(a[key], b[key])) {
return false;
}
}
for (const key in b) {
if (!(key in a) || !areEqualDeep(b[key], a[key])) {
return false;
}
}
return true;
} else {
return a === b;
}
}
const myFirstObject = {};
const mySecondObject = {};
console.log(areEqualDeep(myFirstObject, mySecondObject)); // > true
myFirstObject.hi = "mom";
mySecondObject.hi = "mom";
console.log(areEqualDeep(myFirstObject, mySecondObject)); // > true
myFirstObject.nested = {
hi: "medium",
};
mySecondObject.nested = {
hi: "medium",
};
console.log(areEqualDeep(myFirstObject, mySecondObject)); // > true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment