Hash Tables & JavaScript Objects
Why is a prime number used as the hash table size?
class HashTable {
constructor(size = 53) {
this.keyMap = new Array(size);
}
_hash(key) {
let total = 0;
const WEIRD_PRIME = 31;
for (let i = 0; i < Math.min(key.length, 100); i++) {
total = (total * WEIRD_PRIME + key.charCodeAt(i)) % this.keyMap.length;
}
return total;
}
}