farmhashjs

Pure JavaScript implementation of Google's FarmHash algorithm. No native dependencies, works in Node.js and browsers.
😘 Maintainer: @baptistejamin
Why?
The native farmhash npm package requires C++ compilation, which can be problematic:
- Fails on some platforms (Windows, Alpine Linux, etc.)
- Requires node-gyp and build tools
- Doesn't work in browsers or edge runtimes
farmhashjs provides the same hash outputs with zero native dependencies.
Installation
npm install farmhashjs
Usage
import {
fingerprint64, fingerprint32,
legacyHash64_arm, legacyHash64_x86,
legacyHash32_arm, legacyHash32_x86
} from 'farmhashjs';
fingerprint64('hello world');
fingerprint32('hello world');
legacyHash64_arm('hello world');
legacyHash64_x86('hello world');
legacyHash32_arm('hello world');
legacyHash32_x86('hello world');
API
Modern Functions (Stable Fingerprints)
These produce stable hashes guaranteed to be consistent across all platforms and versions.
fingerprint64(input) | string | 64-bit hash as decimal string |
fingerprint64BigInt(input) | bigint | 64-bit hash as BigInt |
fingerprint32(input) | number | 32-bit hash as number |
Legacy Functions (farmhash v3.x Compatible)
These are compatible with farmhash@3.3.1 which was compiled with FARMHASH_DEBUG=1.
Important: The native farmhash@3.3.1 produces different outputs on ARM64 vs x86_64 architectures. Choose the _arm or _x86 variant based on your target platform.
legacyHash64_arm(input) | string | 64-bit hash (ARM64) |
legacyHash64_x86(input) | string | 64-bit hash (x86_64, <512 bytes) |
legacyHash64BigInt_arm(input) | bigint | 64-bit hash as BigInt (ARM64) |
legacyHash64BigInt_x86(input) | bigint | 64-bit hash as BigInt (x86_64, <512 bytes) |
legacyHash32_arm(input) | number | 32-bit hash (ARM64) |
legacyHash32_x86(input) | number | 32-bit hash (x86_64, <512 bytes) |
Notes:
- For
hash64: ARM64 and x86_64 produce the same output for strings <512 bytes. For ≥512 bytes, different algorithms are used.
- For
hash32: ARM64 and x86_64 produce different outputs for ALL strings (different algorithms).
- The x86_64 SIMD algorithm (farmhashte) is fully implemented in pure JavaScript.
Aliases
import { hash64, hash32 } from 'farmhashjs';
Compatibility
Tested against native implementations:
fingerprint64 | farmhash@5.0.0 fingerprint64 |
fingerprint32 | farmhash@5.0.0 fingerprint32 |
legacyHash64_arm | farmhash@3.3.1 hash64 on ARM64 |
legacyHash64_x86 | farmhash@3.3.1 hash64 on x86_64 (<512 bytes) |
legacyHash32_arm | farmhash@3.3.1 hash32 on ARM64 |
legacyHash32_x86 | farmhash@3.3.1 hash32 on x86_64 (<512 bytes) |
Performance
Benchmarked on Apple M1:
fingerprint32 | 251 ns/op | 903 ns/op | 9 µs/op |
fingerprint64 | 843 ns/op | 7 µs/op | 65 µs/op |
legacyHash32_arm | 302 ns/op | 974 ns/op | 9 µs/op |
legacyHash64_arm | 1.1 µs/op | 6.8 µs/op | 58 µs/op |
32-bit functions are faster because they use native number operations. 64-bit functions use BigInt which has more overhead.
For comparison, native C++ (farmhash@5.0.0) is ~2-10x faster depending on input size.
When to Use
Use farmhashjs when:
- You need cross-platform compatibility
- You're running in browsers, Cloudflare Workers, or edge runtimes
- You can't compile native modules
- You're hashing moderate volumes (< 100K hashes/second)
Use native farmhash when:
- Maximum performance is critical
- You're in a Node.js environment that supports native modules
- You're hashing millions of strings per second
License
Apache-2.0
This is a port of Google's FarmHash. Original FarmHash is Copyright 2014 Google Inc.