minixxh
v0.4.0
Published
xxHash in Javascript
Readme
minixxh
minixxh is a fork of xxhashjs
by Pierre Curto, refactored to use ES Module and Uint8Array.
xxHash is a very fast hashing algorithm, and this library provides pure JavaScript implementation of xxHash32. It is valuable especially in the restrictive environment such as service worker or under strict CSP regulation.
Benchmark
The below is the output of pnpm bench on Apple M1 16GB running
Node.JS v26.6.0 via macOS 26.6 (Darwin Kernel Version 25.6.0).
Warning
minixxh-v8 is an optimised version especially for V8 TurboFan through uint32 computation and escape analysis. These are not suggested for the production use since JIT compiler is not deterministic. The benchmark numbers are not always guaranteed. Also, see the experimental V8 optimisation section.
Input: 64 B x 5,000,000
minixxh 1736.75 MiB/s 0.176s check=0
minixxh-v8 1643.46 MiB/s 0.186s check=0
minixxh-u32le 1475.79 MiB/s 0.207s check=0
xxhash-wasm 1806.80 MiB/s 0.169s check=0
Input: 512 B x 2,500,000
minixxh 2430.34 MiB/s 0.502s check=0
minixxh-v8 4590.26 MiB/s 0.266s check=0
minixxh-u32le 4661.38 MiB/s 0.262s check=0
xxhash-wasm 4793.33 MiB/s 0.255s check=0
Input: 1 KiB x 1,000,000
minixxh 2514.20 MiB/s 0.388s check=0
minixxh-v8 5505.13 MiB/s 0.177s check=0
minixxh-u32le 5523.48 MiB/s 0.177s check=0
xxhash-wasm 5482.08 MiB/s 0.178s check=0
Input: 64 KiB x 20,000
minixxh 2585.82 MiB/s 0.483s check=0
minixxh-v8 6748.43 MiB/s 0.185s check=0
minixxh-u32le 6690.90 MiB/s 0.187s check=0
xxhash-wasm 6222.56 MiB/s 0.201s check=0
Input: 1 MiB x 1,000
minixxh 2602.91 MiB/s 0.384s check=0
minixxh-v8 6732.52 MiB/s 0.149s check=0
minixxh-u32le 6725.09 MiB/s 0.149s check=0
xxhash-wasm 6168.38 MiB/s 0.162s check=0
$ node --import=tsimp bench/xxh32d64.ts
Input: 64 B x 5,000,000
minixxh 1007.24 MiB/s 0.303s check=0x0
Input: 512 B x 2,500,000
minixxh 1960.29 MiB/s 0.623s check=0x0
Input: 1 KiB x 1,000,000
minixxh 2105.51 MiB/s 0.464s check=0x0
Input: 64 KiB x 20,000
minixxh 2251.10 MiB/s 0.555s check=0x0
Input: 1 MiB x 1,000
minixxh 2227.80 MiB/s 0.449s check=0x0Installation
npm install minixxhAPI
xxh32(input: Uint8Array, beg: number, end: number): number
import { xxh32 } from 'minixxh/xxh32';
const input = new TextEncoder().encode('abcd');
const hash = xxh32(input, 0, input.length);
console.log(hash.toString(16));inputmust be aUint8Array.begandendare byte offsets.- The seed is static:
0x9e3779b1. - The return value is an unsigned 32-bit integer.
xxh32d64(input: Uint8Array, beg: number, end: number): bigint
import { xxh32d64 } from 'minixxh/xxh32d64';
const input = new TextEncoder().encode('abcd');
const hash = xxh32d64(input, 0, input.length);
console.log(hash.toString(16));xxh32d64 calculates two xxHash32 values in one pass using two
different static seeds, then merges them into one bigint:
(BigInt(highHash) << 32n) | BigInt(lowHash);inputmust be aUint8Array.begandendare byte offsets.- The high 32-bit seed is static:
0x9e3779b1. - The low 32-bit seed is static:
0x85ebca77. - The return value is an unsigned
bigint.
Experimental V8 optimisation
minixxh provides a set of functions can be used on V8 runtimes:
Node.JS, Chromium, and more. This depends on TurboFan which is
its JIT compiler, and unfortunately it's not deterministic. As
mentioned in the benchmark section at the top of this page, the
numbers in the benchmark are not very likely consistent in the
production environment. Considering that you already have recent
versions of node or Chromium runtime, it might be easier to
depend on xxhash-wasm package for more consistent performance.
This approach still has a clear benefit of not depending on
externals but I cannot tell how accurate tier shaping would work
on any production workflow.
According to the benchmark, it's effective for array sizes of: 64
B >.. . Consider using normal xxh32 function if you have a lot
of small data to process.
import {
xxh32_u32_LE,
xxh32_v8,
createXxh32_v8,
} from 'minixxh/xxh32_v8';
const input = new TextEncoder().encode('abcd');
{
// Converts uint8 array into uint32 array for the faster
// computation, potentially leveraging TurboFan optimisations
// especially in 64 B >.. size ranges (from uint8 perspective).
const hash = xxh32_u32_LE(input, 0, input.length);
console.log(hash.toString(16));
}
{
// Try to automatically selects faster method depending on the
// size of uint8 array.
const hash = xxh32_v8(input, 0, input.length);
console.log(hash.toString(16));
}
{
// Try to warm up code path for the JIT compiler to tier up the
// workflow. This dummy function only calculates some dummy
// hashes to trick the compiler.
// Decide how many times the process should warm up hashing for
// code path tier shaping.
const TIER_SHAPING = 16_384;
const xxh32_v8 = createXxh32_v8(TIER_SHAPING);
const hash = xxh32_v8(input, 0, input.length);
console.log(hash.toString(16));
}Undocumented APIs are compatible with the xxh32 method.
createXxh32_v8(tierShaping: number = 16_384): typeof xxh32
tierShapingmust be anumber.- The seed is static:
0x9e3779b1. - The return value is a
xxh32-compatible function.
License
MIT
