Skip to content

Instantly share code, notes, and snippets.

@Tricky1975
Last active September 7, 2018 14:46
Show Gist options
  • Save Tricky1975/3aa6cad23c4c60dee31ecf04b01bdaf2 to your computer and use it in GitHub Desktop.
Save Tricky1975/3aa6cad23c4c60dee31ecf04b01bdaf2 to your computer and use it in GitHub Desktop.
deepEqual
/*
This is nothing more but a small javascript I wrote for task
I was set out to do, but I stored it here, for personal reasons.
If you hae a use for it, oh well, use it. ;)
*/
function deepEqual(a,b){
if (typeof(a)!=typeof(b)) { return false; }
if (a==null && b==null) { return true; }
if (a==null || b==null) { return false; }
if (typeof(a)=="object") {
if (a==b) return true; // No need to go recursive over the same data, so let's save TIME :P
for (let k of Object.keys(a)) { if (!(k in b)) return false; }
for (let k of Object.keys(b)) { if (!(k in a)) return false; }
let ret=true;
for (let k of Object.keys(a)) ret=ret && deepEqual(a[k],b[k]);
return ret;
}
// In all other cases, just compare!
return a===b;
}
// Test
let 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