Skip to content

Instantly share code, notes, and snippets.

@TimBlock
Created January 15, 2016 08:35
Show Gist options
  • Save TimBlock/77c7c53277e565d7563d to your computer and use it in GitHub Desktop.
Save TimBlock/77c7c53277e565d7563d to your computer and use it in GitHub Desktop.
// Your code here.
function deepEqual(obj1,obj2){
if(obj1===obj2) return true;
if (obj1 == null || typeof obj1 != "object" ||
obj2 == null || typeof obj2 != "object")
return false;
var propObj1 = 0, propObj2 = 0;
for (var prop in obj1)
propObj1 += 1;
for (var prop in obj2) {
propObj2 += 1;
if (!(prop in obj1) || !deepEqual(obj1[prop], obj2[prop]))
return false;
}
return propObj1 == propObj2;
}
var obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// → true
console.log(deepEqual(obj, {here: 1, object: 2}));
// → false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// → true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment