Skip to content

Instantly share code, notes, and snippets.

@tarqd
Last active December 20, 2015 17:49
Show Gist options
  • Save tarqd/6171014 to your computer and use it in GitHub Desktop.
Save tarqd/6171014 to your computer and use it in GitHub Desktop.
Compare objects
var toString = Object.protototype.toString
function objectEquals(x, y) {
// anything that's strictly equal is good
if(x === y) return true;
// at this point any falsy values will not be equal
if(!x || !y) return false;
// valueOf should work for dates
if (x.valueOf() === y.valueOf()) { return true; }
// if one of them is date, they must had equal valueOf
if (x instanceof Date) { return false; }
if (y instanceof Date) { return false; }
// if they are not function or strictly equal, they both need to be Objects
if (!(x instanceof Object)) { return false; }
if (!(y instanceof Object)) { return false; }
var p = Object.keys(x);
return Object.keys(y).every(function (i) { return p.indexOf(i) !== -1; }) ?
p.every(function (i) { return objectEquals(x[i], y[i]); }) : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment