{"id":12694,"library":"xxhashjs","title":"xxHash JavaScript Implementation","description":"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.","status":"maintenance","version":"0.2.2","language":"javascript","source_language":"en","source_url":"https://github.com/pierrec/js-xxhash","tags":["javascript","xxhash","xxh"],"install":[{"cmd":"npm install xxhashjs","lang":"bash","label":"npm"},{"cmd":"yarn add xxhashjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add xxhashjs","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only for Node.js environments. Attempting to use ES module import syntax will fail.","wrong":"import XXH from 'xxhashjs';","symbol":"XXH","correct":"const XXH = require('xxhashjs');"},{"note":"`h32` is a method on the default CommonJS export `XXH`, not a named export. It can be used in a single step or to instantiate a hasher.","wrong":"import { h32 } from 'xxhashjs';","symbol":"XXH.h32","correct":"const XXH = require('xxhashjs');\nconst hash32 = XXH.h32('data', 0);"},{"note":"In browser environments, the library exposes itself as a global variable `_XXH_` when loaded via a script tag. No explicit import or require is needed.","wrong":"const _XXH_ = require('xxhashjs');","symbol":"_XXH_","correct":"// Accessible globally after <script src=\"/your/path/to/xxhash.js\"></script>"}],"quickstart":{"code":"// For Node.js\nconst XXH = require('xxhashjs');\n\n// For browser, XXH is available as _XXH_\n// const XXH = _XXH_;\n\n// Instantiate a 32-bit hasher with a seed in multiple steps\nconst H32 = XXH.h32(0xABCD); // seed = 0xABCD\n\n// Update with data (can be string, ArrayBuffer, or Buffer)\nH32.update('abcd');\n\n// Digest and get the hash value as a hexadecimal string\nconst hashValue32 = H32.digest().toString(16);\n\nconsole.log('32-bit hash:', hashValue32); // Expected: cda8fae4\n\n// The hasher object can be reused with a new or the same seed\nH32.init(0x1234).update('some new data example').digest();\nconsole.log('32-bit hash (reused):', H32.digest().toString(16));\n\n// Example of 64-bit hash in one step\nconst h64Result = XXH.h64('another string example', 0x12345678).toString(16);\nconsole.log('64-bit hash:', h64Result);","lang":"javascript","description":"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."},"warnings":[{"fix":"Be aware of performance characteristics for high-throughput applications. Consider native add-ons if absolute maximum speed is critical.","message":"Performance of xxhashjs is inherently slower than native C/C++ xxHash implementations due to JavaScript's limitations in handling unsigned 32-bit integers and overall execution speed. Do not expect native performance levels.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Ensure you use `const XXH = require('xxhashjs');` for Node.js. If you need to use it in an ESM context, consider using a CommonJS wrapper or a bundler that handles CJS modules.","message":"The package currently only supports CommonJS modules in Node.js environments. There is no official ES module (`import`) support, which can lead to errors in modern JavaScript projects configured for ESM.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"If conflicts arise, load `xxhashjs` in an isolated scope (e.g., an IIFE if possible) or consider manually mapping the global `_XXH_` to another variable immediately after loading.","message":"In browser environments, `xxhashjs` exposes a global variable `_XXH_`. This can potentially conflict with other scripts or libraries that also define a global variable with the same name, leading to unexpected behavior.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Evaluate the project's suitability for long-term critical applications. For actively maintained alternatives, explore other hashing libraries or actively developed xxHash ports.","message":"The project appears to be in a maintenance-only mode with infrequent updates. The last version (0.2.2) was released some time ago, and there may not be active development for new features or prompt bug fixes.","severity":"gotcha","affected_versions":">=0.2.2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to `const XXH = require('xxhashjs');` for Node.js.","cause":"Attempting to use ES module import syntax for a CommonJS-only package in Node.js.","error":"ReferenceError: XXH is not defined (when using 'import XXH from \\'xxhashjs\\'')"},{"fix":"To use `.update()`, first instantiate a hasher: `const H = XXH.h32(seed);` (without data), then call `H.update(data);`.","cause":"You are trying to call `.update()` on a result from a one-step hash calculation. The `update()` method is available only on a hasher instance created for multi-step operations.","error":"TypeError: H.update is not a function (when H = XXH.h32('data', seed))"},{"fix":"Ensure you call `.digest()`: `const hashResult = H.digest(); console.log(hashResult.toString(16));`","cause":"After calling `update()`, you need to call `digest()` on the hasher instance to finalize the calculation and get the hash value before attempting to convert it.","error":"TypeError: Cannot read property 'toString' of undefined (after H.update('data'))"},{"fix":"Verify the script path in your HTML `<script>` tag is correct and that the script loads before any JavaScript code that attempts to use the `_XXH_` global variable.","cause":"The script file for xxhashjs was either not loaded correctly, the path was wrong, or it was loaded after the code trying to access `_XXH_`.","error":"ReferenceError: _XXH_ is not defined (in browser, after <script src=...></script>)"}],"ecosystem":"npm"}