Skip to content

Instantly share code, notes, and snippets.

@ZacharyL2
Created February 27, 2022 04:44
Show Gist options
  • Save ZacharyL2/e6ee3b3e7b664135bfaa22b37a2dbe88 to your computer and use it in GitHub Desktop.
Save ZacharyL2/e6ee3b3e7b664135bfaa22b37a2dbe88 to your computer and use it in GitHub Desktop.
const deepEqual = (objA, objB, map = new WeakMap()) => {
// P1
if (Object.is(objA, objB)) return true;
// P2
if (objA instanceof Date && objB instanceof Date) {
return objA.getTime() === objB.getTime();
}
if (objA instanceof RegExp && objB instanceof RegExp) {
return objA.toString() === objB.toString();
}
// P3
if (
typeof objA !== 'object' ||
objA === null ||
typeof objB !== 'object' ||
objB === null
) {
return false;
}
// P4
if (map.get(objA) === objB) return true;
map.set(objA, objB);
// P5
const keysA = Reflect.ownKeys(objA);
const keysB = Reflect.ownKeys(objB);
if (keysA.length !== keysB.length) {
return false;
}
for (let i = 0; i < keysA.length; i++) {
if (
!Reflect.has(objB, keysA[i]) ||
!deepEqual(objA[keysA[i]], objB[keysA[i]], map)
) {
return false;
}
}
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment