Node.js implementation of FarmHash, Google's family of very fast hash functions
npx @tessl/cli install tessl/npm-farmhash@4.0.0FarmHash is a Node.js implementation of Google's FarmHash family of very fast, non-cryptographic hash functions. It provides both platform-independent fingerprint methods and platform-dependent hash methods with optimizations for different CPU architectures including SSE4.2 and AVX intrinsics.
npm install farmhashconst farmhash = require('farmhash');For ES modules:
import * as farmhash from 'farmhash';const farmhash = require('farmhash');
// Platform-independent fingerprinting
const fingerprint = farmhash.fingerprint32('hello world');
console.log(fingerprint); // number
const fp64 = farmhash.fingerprint64('hello world');
console.log(fp64); // bigint
// Platform-dependent hashing (optimized for current CPU)
const hash = farmhash.hash32('hello world');
console.log(hash); // number
const hashWithSeed = farmhash.hash32WithSeed('hello world', 42);
console.log(hashWithSeed); // numberFarmHash provides two distinct API categories optimized for different use cases:
Both categories support multiple input types (String and Buffer) and provide both 32-bit and 64-bit output options.
Platform-independent fingerprint functions that produce consistent results across different architectures. These are ideal for distributed systems where hash consistency is required.
/**
* Generate 32-bit unsigned integer fingerprint value
* @param input - String or Buffer to fingerprint
* @returns 32-bit unsigned integer fingerprint
* @throws Error if input is not String or Buffer
*/
function fingerprint32(input: string | Buffer): number;
/**
* Generate 64-bit unsigned integer fingerprint value
* @param input - String or Buffer to fingerprint
* @returns 64-bit unsigned integer fingerprint as BigInt
* @throws Error if input is not String or Buffer
*/
function fingerprint64(input: string | Buffer): bigint;
/**
* Generate 64-bit signed integer fingerprint value
* Compatible with Google BigQuery's FARM_FINGERPRINT function
* @param input - String or Buffer to fingerprint
* @returns 64-bit signed integer fingerprint as BigInt
* @throws Error if input is not String or Buffer
*/
function fingerprint64signed(input: string | Buffer): bigint;Usage Examples:
const farmhash = require('farmhash');
// Consistent fingerprints across platforms
const data = 'The quick brown fox';
const fp32 = farmhash.fingerprint32(data);
const fp64 = farmhash.fingerprint64(data);
const fp64signed = farmhash.fingerprint64signed(data);
// Works with Buffers too
const buffer = Buffer.from('binary data', 'utf8');
const bufferFingerprint = farmhash.fingerprint32(buffer);
// BigQuery compatibility
const bqCompatible = farmhash.fingerprint64signed('test data');
console.log(bqCompatible.toString()); // Matches BigQuery FARM_FINGERPRINTPlatform-dependent hash functions optimized for the current CPU architecture. These may produce different results on different machines but offer maximum performance.
/**
* Generate 32-bit unsigned integer hash value
* @param input - String or Buffer to hash
* @returns 32-bit unsigned integer hash
* @throws Error if input is not String or Buffer
*/
function hash32(input: string | Buffer): number;
/**
* Generate 32-bit unsigned integer hash value with seed
* @param input - String or Buffer to hash
* @param seed - Integer seed value
* @returns 32-bit unsigned integer hash
* @throws Error if input is not String or Buffer, or seed is not integer
*/
function hash32WithSeed(input: string | Buffer, seed: number): number;
/**
* Generate 64-bit unsigned integer hash value
* @param input - String or Buffer to hash
* @returns 64-bit unsigned integer hash as BigInt
* @throws Error if input is not String or Buffer
*/
function hash64(input: string | Buffer): bigint;
/**
* Generate 64-bit unsigned integer hash value with seed
* @param input - String or Buffer to hash
* @param seed - Integer seed value
* @returns 64-bit unsigned integer hash as BigInt
* @throws Error if input is not String or Buffer, or seed is not integer
*/
function hash64WithSeed(input: string | Buffer, seed: number): bigint;
/**
* Generate 64-bit unsigned integer hash value with two seeds
* @param input - String or Buffer to hash
* @param seed1 - First integer seed value
* @param seed2 - Second integer seed value
* @returns 64-bit unsigned integer hash as BigInt
* @throws Error if input is not String or Buffer, or seeds are not integers
*/
function hash64WithSeeds(input: string | Buffer, seed1: number, seed2: number): bigint;Usage Examples:
const farmhash = require('farmhash');
// Basic hashing
const data = 'performance critical data';
const hash32 = farmhash.hash32(data);
const hash64 = farmhash.hash64(data);
// Seeded hashing for hash tables
const seed = 12345;
const seededHash = farmhash.hash32WithSeed(data, seed);
// Double-seeded hashing for advanced use cases
const hash2seeds = farmhash.hash64WithSeeds(data, 111, 222);
// Buffer hashing for binary data
const binaryData = Buffer.from([0x01, 0x02, 0x03, 0x04]);
const binaryHash = farmhash.hash64(binaryData);All functions validate their inputs and throw descriptive errors:
Error('Expected a String or Buffer for input') if input is not String or BufferError('Expected an integer for seed') if seed parameter is not an integerconst farmhash = require('farmhash');
try {
// This will throw - invalid input type
farmhash.hash32(123);
} catch (error) {
console.log(error.message); // "Expected a String or Buffer for input"
}
try {
// This will throw - invalid seed type
farmhash.hash32WithSeed('data', 'not a number');
} catch (error) {
console.log(error.message); // "Expected an integer for seed"
}npm install --build-from-source to enable additional CPU-specific optimizations (AVX intrinsics)