farmhashjs
v1.0.1
Published
Pure JavaScript implementation of Google's FarmHash - compatible with farmhash v3.x and v5.x
Readme
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 farmhashjsUsage
import {
fingerprint64, fingerprint32,
legacyHash64_arm, legacyHash64_x86,
legacyHash32_arm, legacyHash32_x86
} from 'farmhashjs';
// Modern stable fingerprints (recommended - same on all platforms)
fingerprint64('hello world'); // "6381520714923946011"
fingerprint32('hello world'); // 430397466
// Legacy hashes (compatible with [email protected])
// Use _arm or _x86 suffix based on your target platform
legacyHash64_arm('hello world'); // "16022978042064026561"
legacyHash64_x86('hello world'); // "16022978042064026561" (same for <512 bytes)
legacyHash32_arm('hello world'); // 3314386015
legacyHash32_x86('hello world'); // 1955099599API
Modern Functions (Stable Fingerprints)
These produce stable hashes guaranteed to be consistent across all platforms and versions.
| Function | Returns | Description |
|----------|---------|-------------|
| 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 [email protected] which was compiled with FARMHASH_DEBUG=1.
Important: The native [email protected] produces different outputs on ARM64 vs x86_64 architectures. Choose the _arm or _x86 variant based on your target platform.
| Function | Returns | Description |
|----------|---------|-------------|
| 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';
// hash64 = fingerprint64
// hash32 = fingerprint32Compatibility
Tested against native implementations:
| Function | Compared Against |
|----------|------------------|
| fingerprint64 | [email protected] fingerprint64 |
| fingerprint32 | [email protected] fingerprint32 |
| legacyHash64_arm | [email protected] hash64 on ARM64 |
| legacyHash64_x86 | [email protected] hash64 on x86_64 (<512 bytes) |
| legacyHash32_arm | [email protected] hash32 on ARM64 |
| legacyHash32_x86 | [email protected] hash32 on x86_64 (<512 bytes) |
Performance
Benchmarked on Apple M1:
| Function | 44 bytes | 500 bytes | 5 KB |
|----------|----------|-----------|------|
| 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++ ([email protected]) 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.
