Skip to content

Instantly share code, notes, and snippets.

@A1rPun
Last active August 17, 2017 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save A1rPun/c739b0bfe1450efbc50e67668942ce19 to your computer and use it in GitHub Desktop.
Save A1rPun/c739b0bfe1450efbc50e67668942ce19 to your computer and use it in GitHub Desktop.
Javascript value equality
function getHashCode(obj) {
var hashCode = 0;
if (typeof obj === 'object')
for (var prop in obj) {
var code = getHashCode(prop) + getHashCode(obj[prop]);
hashCode += code * code;
}
else
for(var str = "" + obj, i = str.length; i--;)
hashCode += 100 / (i + str.charCodeAt(i));
return hashCode;
}
var someObj = getHashCode({ id: 1, name: 'Pooka', isCat: true }); // 166.3732793411292
var sameObj = getHashCode({ isCat: true, name: 'Pooka', id: 1 }); // 166.3732793411292
if (someObj === sameObj) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment