Skip to content

Instantly share code, notes, and snippets.

@victorcolina22
Last active December 17, 2025 22:39
Show Gist options
  • Select an option

  • Save victorcolina22/b4505dc685ea1bd93c0acfbdf699eb38 to your computer and use it in GitHub Desktop.

Select an option

Save victorcolina22/b4505dc685ea1bd93c0acfbdf699eb38 to your computer and use it in GitHub Desktop.

Deep compare function in TypeScript

export const deepCompare = (objA: any, objB: any) => {
	if (objA === objB) return true; // Misma referencia o valores primitivos

	if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
		return false; // Ningún objeto es null
	}

	const keysA = Object.keys(objA);
	const keysB = Object.keys(objB);

	if (keysA.length !== keysB.length) return false; // Diferente número de propiedades

	for (const key of keysA) {
		if (!keysB.includes(key) || !deepCompare(objA[key], objB[key])) {
			return false; // Si falta una key o sus values no coinciden
		}
	}
	return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment