npm.io
1.0.0 • Published 1 month ago

fast-fnv1a

Licence
MIT
Version
1.0.0
Deps
0
Size
9 kB
Vulns
0
Weekly
0
Stars
1

fast-fnv1a

A fast 32-bit FNV-1a hash implementation for JavaScript, up to 6x faster than alternatives. The same implementation is adopted by fastify-etag and see 2x performance boost with large responses.

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).

The classic 32-bit FNV-1a is usually written with a chain of shifts and adds to emulate the 32-bit multiply. That forces the value out of the small-integer range on every character, so the engine keeps boxing it as a double. This implementation instead carries the hash as two 16-bit halves, which stay inside the small-integer range throughout — making it up to 6x faster.

If you need a larger hash space, check out fnv1a52 for 52 bits (still a plain number, w/o BigInt), or @danielroe's fnv1a-64 for full 64 bits hash space.

Install

pnpm add fast-fnv1a
yarn add fast-fnv1a
npm install fast-fnv1a

Usage

import { fnv1a, fnv1ahex } from 'fast-fnv1a';

console.log(fnv1a('hello world'));
//=> 3582672807

console.log(fnv1ahex('hello world'));
//=> 'd58b3fa7'

You should NEVER fnv1a().toString(16)! fnv1ahex can get you a fixed length zero-padded hex string up to 3.9x faster via a byte-to-hex lookup table.

API

fnv1a(str)

Returns the 32-bit FNV-1a hash of str as an unsigned number.

  • str: string
fnv1ahex(str)

Returns the 32-bit FNV-1a hash of str as a lowercase hexadecimal string (fixed length, zero-padded to 8 characters).

  • str: string

Prefer this over fnv1a(str).toString(16) — it uses a byte-to-hex lookup table and is up to 3.9x faster.

Note the fixed length means this is not always equal to fnv1a(str).toString(16): a hash below 16 ** 7 keeps its leading zero here, while toString(16) drops it.

Benchmark

Hashing 'the quick brown fox jumps over the lazy dog', compared against fnv1a and @sindresorhus/fnv1a:

$ pnpm run bench

clk: ~3.27 GHz
cpu: Apple M2 Max
runtime: node 24.18.0 (arm64-darwin)

• fnv1a
------------------------------------------------- -------------------------------
fnv1a                              298.12 ns/iter
@sindresorhus/fnv1a                  1.14 µs/iter
fast-fnv1a                          69.17 ns/iter

• hex
------------------------------------------------- -------------------------------
fnv1a + toString(16)               627.36 ns/iter
@sindresorhus/fnv1a + toString(16)   1.10 µs/iter
fast-fnv1a + toString(16)          356.68 ns/iter
fnv1ahex                            92.01 ns/iter

Note that @sindresorhus/fnv1a hashes UTF-8 bytes and returns a BigInt, so it is not a drop-in equivalent — it produces different values for non-BMP input and costs an allocation per call. fnv1a and fast-fnv1a both hash UTF-16 code units and agree on all inputs.

License

MIT


fast-fnv1a 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

Keywords