xxHash JavaScript Implementation

0.2.2 · maintenance · verified Sun Apr 19

xxhashjs is a pure JavaScript implementation of the xxHash fast hashing algorithm. Currently at version 0.2.2, the library provides both 32-bit and 64-bit hashing capabilities, a feature introduced in version 0.2.0. While it aims to replicate the speed of the original C implementation, it acknowledges performance limitations inherent to JavaScript's handling of unsigned 32-bit integers. The project appears to be in a maintenance phase, with infrequent updates since its last release. It supports both Node.js environments via CommonJS and browser environments through a global `_XXH_` object, offering a convenient way to compute hashes in single or multiple steps for various data types including strings, ArrayBuffers, and Node.js Buffers.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates both 32-bit and 64-bit xxHash calculations, including multi-step hashing with object reuse, suitable for Node.js CommonJS or browser global usage.

// For Node.js
const XXH = require('xxhashjs');

// For browser, XXH is available as _XXH_
// const XXH = _XXH_;

// Instantiate a 32-bit hasher with a seed in multiple steps
const H32 = XXH.h32(0xABCD); // seed = 0xABCD

// Update with data (can be string, ArrayBuffer, or Buffer)
H32.update('abcd');

// Digest and get the hash value as a hexadecimal string
const hashValue32 = H32.digest().toString(16);

console.log('32-bit hash:', hashValue32); // Expected: cda8fae4

// The hasher object can be reused with a new or the same seed
H32.init(0x1234).update('some new data example').digest();
console.log('32-bit hash (reused):', H32.digest().toString(16));

// Example of 64-bit hash in one step
const h64Result = XXH.h64('another string example', 0x12345678).toString(16);
console.log('64-bit hash:', h64Result);

view raw JSON →