fnv1a52
A fast 52-bit FNV-1a hash implementation for JavaScript
52 bits provides a larger hash space than the classic 32-bit FNV-1a while still fitting entirely inside JavaScript's 53-bit safe integer range (double precision) — so the result is a plain number, no BigInt required. Zero dependencies.
The implementation is based on prior work by @tjwebb (Travis Webb) and @desudesutalk under the MIT license, with extra simplifacations and optimizations (like inline lookup table and faster hex conversion).
If you need full 64 bits hash space, check out @danielroe's https://github.com/danielroe/fnv1a-64
Install
pnpm add fnv1a52
yarn add fnv1a52
npm install fnv1a52
Usage
import { fnv1a52, fnv1a52base36, fnv1a52hex } from 'fnv1a52';
console.log(fnv1a52('hello world'));
//=> 2926792616498590
console.log(fnv1a52hex('hello world'));
//=> 'a65e7023cd59e'
console.log(fnv1a52base36('hello world'));
//=> '0stglysbf6m'
You should NEVER
fnv1a52().toString(16)!fnv1a52hexcan get you a fixed length zero-padded hex string up to 6x faster via a byte-to-hex lookup table.
The same applies to
fnv1a52().toString(36)— always usefnv1a52base36instead, which can get you a fixed length zero-padded hex string up to 3x faster.
API
fnv1a52(str)
Returns the 52-bit FNV-1a hash of str as a number.
- str:
string
fnv1a52hex(str)
Returns the 52-bit FNV-1a hash of str as a lowercase hexadecimal string (fixed length, zero-padded).
- str:
string
Prefer this over fnv1a52(str).toString(16) — it uses a byte-to-hex lookup table and is up to 6x faster.
Note the fixed length means this is not always equal to fnv1a52(str).toString(16): a hash below 16 ** 12 keeps its leading zero here, while toString(16) drops it.
fnv1a52base36(str)
Returns the 52-bit FNV-1a hash of str as a lowercase base36 string (fixed length, zero-padded).
- str:
string
Prefer this over fnv1a52(str).toString(36) as it is up to 3x faster.
Benchmark
$ pnpm run bench
clk: ~3.15 GHz
cpu: Apple M2 Max
runtime: node 24.18.0 (arm64-darwin)
• fnv1a52 - hex
------------------------------------------------ -------------------------------
fnv1a52 + toString(16) 656.58 ns/iter
fnv1a52hex 120.82 ns/iter
• fnv1a52 - base36
------------------------------------------------ -------------------------------
fnv1a52 + toString(36) + padStart 506.29 ns/iter
fnv1a52base36 137.43 ns/iter
License
fnv1a52 Sukka, Released under the MIT License. Authored and maintained by Sukka with help from contributors (list).
Personal Website · Blog · GitHub @SukkaW · Telegram Channel @SukkaChannel · Mastodon @sukka@acg.mn · Twitter @isukkaw · BlueSky @skk.moe