@httpx/hash
v0.2.4
Published
Collection of hash functions
Readme
@httpx/hash
Install
$ npm install @httpx/hash
$ yarn add @httpx/hash
$ pnpm add @httpx/hashFeatures
- 🖖 Provides
- 🚀 Fast
- 📐 Lightweight (starts at ~570B)
- 🛡️ Tested on node 20-24, bun, browser, cloudflare workers and runtime/edge.
- 🗝️ Available in ESM and CJS formats.
Documentation
XXHash64 (wasm)
The XXHash is a very fast non-cryptographic hash algorithm. It is designed to be extremely fast while maintaining a low collision rate.
The XXHash64 variant produces a 64-bit hash suitable for checksums, hash tables and databases.
- [x] Extremely fast.
- [x] Low collision rate.
- [x] 64-bit output (bigint, signed64).
- [x] Passes SMHasher tests.
- [x] Doesn't fully pass SMHasher3 tests (but still good enough for non-cryptographic use cases).
Check SMHasher and SMHasher3 for more details about quality of hash functions.
Usage
For most projects, you can benefit from top-level await by creating a file named xxHash64.ts.
The file will initialize the wasm module and export the hasher instance without async calls in
the rest of your code.
import { createXXHash64 } from '@httpx/hash/xxhash-wasm';
// Notice the top-level await as wasm loading is async.
const xxHash64 = await createXXHash64({
// Optionally provide a seed (default is 0n)
// For example, Spark uses 42 as a default seed
defaultSeed: 0n,
});
Use it as follows:
import { xxHash64 } from './xxHash64';
// Javascript Bigint output as 64-bit unsigned integer
const hashedBigint = xxHash64.toBigint('some input string');
// Javascript Bigint output as 64-bit signed integer
// Same as `BigInt.asIntN(64, xxHash64.toBigint('some input string'))`
const hashedSigned64 = xxHash64.toSigned64('some input string');Benchmarks
Performance is continuously monitored thanks to codspeed.io.
RUN v3.2.4 /home/sebastien/github/httpx/packages/hash
✓ bench/compare/xxhash.bench.ts > xxHash64 2823ms
name hz min max mean p75 p99 p995 p999 rme samples
· toBigint 1,741,422.57 0.0002 1.0043 0.0006 0.0007 0.0011 0.0013 0.0039 ±0.89% 870712
· toSigned64 1,630,206.76 0.0003 0.8919 0.0006 0.0007 0.0013 0.0015 0.0079 ±1.17% 815106See benchmark file for details.
Bundle size
Bundle size is tracked by a size-limit configuration
| Scenario (esm) | Size (compressed) |
|------------------------------------------------------------|------------------:|
| import { createXXHash64 } from '@httpx/hash/xxhash-wasm' | ~ 765B (*) |
(*) Add 2.3KB gzipped for the wasm file.
Compatibility
| Level | CI | Description |
|--------------|----|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Node | ✅ | CI for 20.x, 22.x, 24.x & 25.x. |
| Browser | ✅ | Tested with latest chrome (vitest/playwright) |
| Browserslist | ✅ | > 95% on 01/2025. defaults, chrome >= 96, firefox >= 105, edge >= 113, safari >= 15, ios >= 15, opera >= 103, not dead |
| Edge | ✅ | Ensured on CI with @vercel/edge-runtime. |
| Bun | ✅ | Tested with latest (at time of writing >= 1.3.3) |
| Cloudflare | ✅ | Ensured with @cloudflare/vitest-pool-workers (see wrangler.toml |
| Typescript | ✅ | TS 5.0 + / are-the-type-wrong checks on CI. |
| ES2022 | ✅ | Dist files checked with es-check |
| Performance | ✅ | Monitored with codspeed.io |
For older browsers: most frontend frameworks can transpile the library (ie: nextjs...)
Comparison with other libraries
Contributors
Contributions are welcome. Have a look to the CONTRIBUTING document.
Sponsors
If my OSS work brightens your day, let's take it to new heights together! Sponsor, coffee, or star – any gesture of support fuels my passion to improve. Thanks for being awesome! 🙏❤️
Special thanks to
License
MIT © Sébastien Vanvelthem and contributors.
