Skip to content

Instantly share code, notes, and snippets.

@kirpalmakanga
Last active May 28, 2025 14:55
Show Gist options
  • Save kirpalmakanga/a92bb22091aa59433fcbce11e11653a0 to your computer and use it in GitHub Desktop.
Save kirpalmakanga/a92bb22091aa59433fcbce11e11653a0 to your computer and use it in GitHub Desktop.
isEqual
export function isEqual(a: unknown, b: unknown): boolean {
if (a === b) return true;
if (a == null || b == null) return a === b;
if (typeof a !== typeof b) return false;
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) return false;
return a.every((item, index) => isEqual(item, b[index]));
}
if (typeof a === 'object' && typeof b === 'object') {
const keysA = Object.keys(a) as (keyof typeof a)[];
const keysB = Object.keys(b) as (keyof typeof b)[];
if (keysA.length !== keysB.length) return false;
return keysA.every((key) => key in b && isEqual(a[key], b[key]));
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment