Skip to content

Commit

Permalink
Consolidating hash logic.
Browse files Browse the repository at this point in the history
Cleaning up hash.js a litle bit, and moving the `% MAX_HASHCODE`
operation to the end. Doesn't make sense to do it in the loops as both
values are 32-bits, and xoring them will still keep them 32 bits.
  • Loading branch information
stevekrenzel committed Sep 5, 2018
1 parent f54c11c commit 0976ca0
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/utils/set/hash.js
Expand Up @@ -43,28 +43,30 @@ function hash(value) {
return intHash(value);
}

let code = 0;

if (typeof(value) === 'string') {
let code = 0;

const upper = Math.min(value.length, MAX_STRING_LEN);
for (let i = 0; i < upper; i++) {
code += (intHash(i) ^ intHash(value.charCodeAt(i))) % MAX_HASHCODE;
code += intHash(i) ^ intHash(value.charCodeAt(i));
}
return code;
}

if (isArray(value)) {
let code = 0;
} else if (isArray(value)) {

for(let i = 0; i < value.length; i++) {
code += (intHash(i) ^ hash(value[i])) % MAX_HASHCODE;
code += intHash(i) ^ hash(value[i]);
}

} else {

for (const key in value) {
code += hash(key) ^ hash(value[key]);
}
return code;
}

let code = 0;
for (const key in value) {
code += (hash(key) ^ hash(value[key])) % MAX_HASHCODE;
}
return code;

return code % MAX_HASHCODE;

}

Expand Down

0 comments on commit 0976ca0

Please sign in to comment.