Javascript Implementation of Jenkins' Hash
Libmemcached can use a cluster of servers as a backend. Each key is stored/read on only one server. The client determines which server to store/read each key based on a hash of the key. The hash value is a 32-bit integer whose modulus with the number of servers results in an integer in the range (0, number of servers). This is a pretty simple, but thoroughly useful, way to (roughly and likely mostly) evenly bucketize strings.
My needs required getting the same hash value in Javascript for a node.js project I’m working on. There were a few questionable attempts at some of the Jenkins hash functions, but all looked somewhat dubious. I decided to implement the simplest, the “one-at-a-time” hash function, which seemed to be sufficient for my hashing needs (and which is the default hash function in libmemcached).
So here’s the function. It accepts a string key and the interval size to use as a modulus for the integer hash value.