Skip to content

Instantly share code, notes, and snippets.

@kinngh
Created June 13, 2025 00:58
Show Gist options
  • Save kinngh/3135166de43737aac084faa609844511 to your computer and use it in GitHub Desktop.
Save kinngh/3135166de43737aac084faa609844511 to your computer and use it in GitHub Desktop.
Recursively see if an object is exactly equal to another object
/**
* Recursively checks if 2 given objects are equal.
* Used for SaveBar comparison.
*
* @param {Object} obj1
* @param {Object} obj2
* @returns {Boolean}
*/
const isEqual = (obj1, obj2) => {
if (obj1 === obj2) return true;
if (
typeof obj1 !== "object" ||
obj1 === null ||
typeof obj2 !== "object" ||
obj2 === null
) {
return obj1 === obj2;
}
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) return false;
for (const key of keys1) {
if (!keys2.includes(key) || !isEqual(obj1[key], obj2[key])) {
return false;
}
}
return true;
};
export default isEqual;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment