Skip to content

About

Hashes subjects for stable A/B assignment.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

17 Commits

Folders and files

Repository files navigation

Bucket Split\n\nStable, zero-dependency hash-based A/B bucket assignment for JavaScript.\n\n## Usage\n\njs\nimport { assign } from 'bucket-split';\n\nconst bucket = assign('user-12345', [3, 7]); // 0 or 1, ~30/70 split\n\n\nassign(subject, buckets) takes a subject identifier (string or anything\nstringifiable) and an array of positive relative bucket sizes. It returns the\n0-based index of the bucket the subject falls into. buckets describes\nproportions, not counts: [1,1] is 50/50, [3,7] is 30/70.\n\nAlso exported: hashSubject(subject) for the raw 32-bit FNV-1a hash, and\nbucketsToThresholds(buckets) for the cumulative [0, 1] boundaries derived\nfrom a bucket list.\n\n## Why this exists\n\nA/B tests need subjects to land in the same bucket on every evaluation, with\nno central coordination. A hash gives you that for free. The trade-off is that\nthe split is only approximately the requested ratio — FNV-1a is fast and\ndependency-free but not cryptographically uniform, so small samples drift from\nthe target percentages. If you need exact counts or cryptographic randomness,\nthis is the wrong tool.\n\n## Edge cases\n\n- null and undefined subjects throw. Numbers and booleans are stringified\n before hashing, so assign(123, [1,1]) and assign('123', [1,1]) agree.\n- Bucket sizes must be positive finite numbers; an empty or all-zero list\n throws rather than silently producing a meaningless assignment.\n- The last bucket's upper threshold is fixed at 1, so a subject whose\n normalised hash is very close to 1 still lands in the final bucket rather\n than escaping the range.\n\n## Running the tests\n\nsh\nnode --test\n\n\nESM only. No transpilation, no dependencies.\n

Performance

The window keeps a bounded buffer, so push is constant time and memory does not grow with the length of the stream. peak and trough are linear in the window size, which is the trade that keeps push cheap.

Limitations

Values are coerced to floats, so very large integers lose precision. If you need exact integer aggregates over a window, this is the wrong tool.

Contributing

Issues and pull requests are welcome. Please keep the dependency list empty — that constraint is the point of the project, not an oversight.

Design notes

The window stores values eagerly rather than keeping running aggregates. Running sums drift with floating point over long streams, and recomputing from a small buffer is cheap enough that the drift is not worth the speed.

About

Hashes subjects for stable A/B assignment.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages