Disqus

Software Discourse.

Implementation: Client-side JavaScript bucketing

Posted by Gabriel Fouasnon on

Postscript to The paper plane proof: our uplifting story of client-side JavaScript bucketing

The Implementation

show me the codez

There are basically two parts to our bucketing:

  1. Generating a random number
  2. Hashing the random number into a discrete range – i.e., 0-99 inclusive

Result:

function getBucket() {
    // generate part-random session id
    var bucket = generateSessionId();

    // hash id string into discrete range
    bucket = Math.abs(hash(id)) % 100;

    return bucket;
}

It’s important to note that we did not build all of the bucketing code from scratch. Instead we opportunistically leveraged existing code. If we were to build this from scratch, this is probably not how we would do it. By extension, it’s almost certainly not exactly how you should do it.

If we had been building from scratch, the code probably would have looked more like this:

Client-side id generation

Client id helper functions

Hashing a string to an integer

from Stack Overflow


Got a question about the implementation? Ask in the comments below.

comments powered by Disqus