Skip to content

Instantly share code, notes, and snippets.

@wcang
Created December 20, 2014 06:49
Show Gist options
  • Save wcang/cbd1d02ec97f8f1f12ec to your computer and use it in GitHub Desktop.
Save wcang/cbd1d02ec97f8f1f12ec to your computer and use it in GitHub Desktop.
Deep comparison in Javascript
function deepEqual(obj1, obj2)
{
if (obj1 === obj2) {
return true;
}
if (typeof(obj1) == typeof(obj2)) {
if (typeof(obj1) == 'object') {
var obj1Prop = 0;
var obj2Prop = 0;
for (var prop in obj1) {
obj1Prop++;
}
for (var prop in obj2) {
obj2Prop++;
}
if (obj1Prop != obj2Prop)
return false;
for (var prop in obj1) {
if (prop in obj2) {
if (!deepEqual(obj1[prop], obj2[prop]))
return false;
}
else {
return false;
}
}
return true;
}
else {
return (isNaN(obj1) && isNaN(obj2)) || obj1 === obj2;
}
}
else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment