{"name":"xxhash-addon","version":"1.0.3","keywords":["xxhash","n-api","napi"],"author":{"name":"Nhan Khong","email":"knhana7@gmail.com"},"license":"BSD-2-Clause","_id":"xxhash-addon@1.0.3","maintainers":[{"name":"ktrongnhan","email":"knhana7@gmail.com"}],"homepage":"https://github.com/ktrongnhan/xxhash-addon#readme","bugs":{"url":"https://github.com/ktrongnhan/xxhash-addon/issues"},"dist":{"shasum":"76bba737e89dfcec8565268902bae340addea8bf","tarball":"http://registry-npm.ywwl.com/xxhash-addon/download/xxhash-addon-1.0.3.tgz","fileCount":17,"integrity":"sha512-4bUnKrqjpd8nDP1KpAKGteS0fmVwRdIi8Pcdxf8s99YY6cNP7acXUMaIAYoC1nuWX5KX8OM3d+7D2AT9wXz0AQ==","signatures":[{"sig":"MEYCIQCur4qxqpwrnQJFaj70PFDfBgOMhTbW9qJ7cYquCOanQQIhAPzabm6CBbyp+NxJnF0Xc/e9KOwhdbOqLpiLkfOiQtPK","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":178254,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJd/wPtCRA9TVsSAnZWagAAev4QAKSvZvlpadkMogR0km7V\nqIaYZgmW6XHIIA7T6EDmpAfidBRwoggA+6uhpF4i5Fh1Iv8J2aBwsxITdTMP\nFXuG8PE+8K/BuvMIRbMan+tu7qAYMKN7bRUBah+div541LW/YRTyEvBTd1es\nC7gf5U83PV+Vl5hgdPFhyhnILO5CUVOi9qABBt0QkxI/ZDloC5cmvH6Ry/7B\nQ6X490pZr0VR+0Hjyvjr82r8+8kmDd5lhwNmWrU4nS9igHDo1Yin5z2y30cU\nXJIC7LWCXhXdME39d1b4S95zdnXphU0brXU/rulzyOsWyBTVW44SQ4YgzYN/\nhgqbZbTW8ZoYOFD3tbx+ZoKCHZrTOzUKkKKcKWOPHlOBJcw4H9UGvihzN/GR\n2dR0pfU7khJmNOBwfC5MjJOOi4d0Fu6dK4D3hsNOW5q/w+rI+GR33LMLSnvQ\nvcJA6Tycjsb2vI2O/RChhhxjIEvxRMlEUi8MwZmzinFK3mwL/YYKydOxyqQC\nTrd8Egn/LB77Th2lV3thipSiS/FAFXCaoVrA+su/XKA6ZduJj8x7aNxTXxBl\nkhhVMhXu2qC4zV26qawZ1GhrUpsO7lYJb2J+UVIMFJWnGOZ20ZVfOponG/Yl\nqbvaRBH+JlAC/vmOsUwS5qTPspiM48+x3A8BY19BhowxnMAZVHXChVdK6aMP\nkFKq\r\n=MLXX\r\n-----END PGP SIGNATURE-----\r\n","size":41527,"noattachment":false,"key":"/xxhash-addon/-/xxhash-addon-1.0.3.tgz"},"main":"xxhash-addon.js","gitHead":"d72a12923f62f69e9cd11cc0a0d73021abb1aa13","scripts":{"test":"jest","install":"node-gyp rebuild --target=v8.6.0 --verbose --ensure"},"_npmUser":{"name":"ktrongnhan","email":"knhana7@gmail.com"},"repository":{"url":"git+https://github.com/ktrongnhan/xxhash-addon.git","type":"git"},"_npmVersion":"6.10.3","description":"Yet another xxhash addon for Node.js","directories":{},"_nodeVersion":"12.10.0","_hasShrinkwrap":false,"devDependencies":{"jest":"^24.9.0"},"_npmOperationalInternal":{"tmp":"tmp/xxhash-addon_1.0.3_1576993773169_0.10263813338698724","host":"s3://npm-registry-packages"},"readme":"Yet another `xxhash` addon for Node.js which can be ***x50 times faster*** than `crypto` MD5\n\n__IMPORTANT__: `xxhash-addon` v2 is finally here. This is almost a re-work of this project with heavy focus on performance and consistency. [FAQ](https://github.com/ktrongnhan/xxhash-addon#FAQ) has some very good info that you may not want to miss.\n\n![npm](https://img.shields.io/npm/v/xxhash-addon?style=plastic)\n![NPM](https://img.shields.io/npm/l/xxhash-addon?style=plastic)\n\n[![CI](https://github.com/ktrongnhan/xxhash-addon/actions/workflows/ci.yml/badge.svg)](https://github.com/ktrongnhan/xxhash-addon/actions/workflows/ci.yml)\n[![Benchmarks](https://github.com/ktrongnhan/xxhash-addon/actions/workflows/benchmark.yml/badge.svg)](https://github.com/ktrongnhan/xxhash-addon/actions/workflows/benchmark.yml)\n\nOverview\n===========\n`xxhash-addon` is a native addon for Node.js (>=8.6.0) written using N-API. It 'thinly' wraps [xxhash](https://github.com/Cyan4973/xxHash) `v0.8.3`, which has support for a new algorithm `XXH3` that has been showed to outperform its predecessor.\n\n__IMPORTANT__: As of `v0.8.0`, XXH3 and XXH128 are now considered stable. Rush to the upstream [CHANGELOG](https://github.com/Cyan4973/xxHash/blob/v0.8.0/CHANGELOG) for the formal announcement! `xxhash-addon v1.4.0` is the first iteration packed with stable XXH3 and XXH128.\n\nWhy v2?\n==========\n1. Greatly improved performance backed by benchmarks (see charts below.)\n2. Better consistency and smaller code size thanks to pure C-style wrapping.\n\n\nCI Benchmarks\n==========\n\nBenchmarks run automatically on every push to `master` and every pull request across multiple platforms, compilers, architectures, and Node.js versions. Results are published as a [GitHub Actions Job Summary](https://github.com/ktrongnhan/xxhash-addon/actions/workflows/benchmark.yml) — click any workflow run to see the consolidated throughput table.\n\n**Coverage:** Linux x86_64/ARM64 (GCC + Clang), macOS ARM64 (Clang), Windows x86_64/ARM64 (MSVC), Node.js 22 + 24.\n\nThe benchmark measures both **streaming** (`update()` + `digest()`) and **one-shot** (`hash()`) throughput across buffer sizes from 1 KB to 16 MB. Iterations are auto-tuned to ~1 s per measurement, with 2 warmup runs and 5 measured runs, reporting median throughput in GB/s. The headline table below shows streaming throughput at 64 KB chunks — the default `fs.createReadStream` buffer size.\n\nTo run locally:\n```bash\nnpm run benchmark\n```\n\nTo save results as JSON:\n```bash\nBENCHMARK_OUTPUT=results.json node benchmark.js\n```\n\n<!-- BENCHMARK_RESULTS_START -->\n## Benchmark Results\n\n> Streaming throughput in GB/s at 64 KB chunks (higher is better). Median of 5 runs.\n\n| Platform | Compiler | Node | XXH64 | XXH3 | XXH128 | MD5 | SHA1 |\n|----------|----------|------|------:|------:|------:|------:|------:|\n| macOS arm64 | Apple Clang 17.0.0 | v24 | 12.55 | 33.52 | 32.23 | 0.65 | 2.14 |\n| macOS x64 | Apple Clang 17.0.0 | v24 | 14.53 | 35.67 | 36.73 | 0.83 | 1.09 |\n| Linux arm64 | GCC 13.3.0 | v24 | 20.97 | 19.88 | 19.85 | 0.70 | 1.94 |\n| Linux arm64 | Clang 18.1.3 | v24 | 19.01 | 20.08 | 20.06 | 0.70 | 1.94 |\n| Linux x64 | GCC 13.3.0 | v24 | 11.61 | 40.58 | 40.57 | 0.66 | 1.58 |\n| Linux x64 | Clang 18.1.3 | v24 | 11.60 | 41.72 | 41.40 | 0.66 | 1.58 |\n| Windows arm64 | MSVC 17.14.36811.4 | v24 | 20.59 | 9.94 | 9.93 | 0.66 | 0.74 |\n| Windows x64 | MSVC 17.14.37012.4 | v24 | 11.42 | 38.47 | 38.26 | 0.66 | 1.57 |\n\n*Generated at 2026-03-01T16:53:33.255Z with xxHash v0.8.3*\n\n<!-- BENCHMARK_RESULTS_END -->\n\n\nFeatures\n==========\n* `xxhash-addon` exposes xxhash's API in a friendly way for downstream consumption (see the Example of Usage section).\n* Covering all 4 variants of the algorithm: XXH32, XXH64, XXH3 64-bit, XXH3 128-bit.\n* Supporting XXH3 secret.\n* Consistently producing canonical (big-endian) form of hash values as per [xxhash's recommendation](https://github.com/Cyan4973/xxHash/blob/e2f4695899e831171ecd2e780078474712ea61d3/xxhash.h#L243).\n* The addon is extensively sanity-checked againts xxhash's sanity test suite to ensure that generated hashes are correct and align with xxhsum's (`xxhsum` is the official utility of xxhash). Check the file `xxhash-addon.test.js` to see how `xxhash-addon` is being tested.\n* Being seriously checked against memory safety and UB issues with ASan and UBSan. See [the CI](https://github.com/ktrongnhan/xxhash-addon/actions/workflows/ci.yml) for how this is done.\n* Benchmarks are publicly available.\n* Minimal dependency: one tiny runtime dependency ([`node-gyp-build`](https://github.com/prebuild/node-gyp-build), 3 KB, zero transitive dependencies).\n* TypeScript support. `xxhash-addon` **is strongly recommended** to be used with TypeScript. Definitely check [FAQ](https://github.com/ktrongnhan/xxhash-addon#FAQ) before using the addon.\n\nInstallation\n=========\n```bash\nnpm install xxhash-addon\n```\n\nPrebuilt binaries are included for the following platforms -- no C compiler required:\n\n| Platform | Architecture |\n|----------|-------------|\n| Linux (glibc) | x64, arm64 |\n| Linux (musl/Alpine) | x64, arm64 |\n| macOS | x64 (Intel), arm64 (Apple Silicon) |\n| Windows | x64, arm64 |\n\nFor other platforms, the addon falls back to compiling from source via `node-gyp`, which requires a C toolchain:\n\nOn a Windows machine\n\n```bash\nnpm install --global --production windows-build-tools\n```\n\nOn a Debian/Ubuntu machine\n\n```bash\nsudo apt-get update && sudo apt-get install python g++ make\n```\n\nOn a RHEL/CentOS machine\n\nIf you are on RHEL 6 or 7, you would need to install GCC/G++ >= 6.3 via `devtoolset-` for the module to compile. See [SCL](https://www.softwarecollections.org/en/scls/rhscl/devtoolset-6/).\n\nOn a Mac\n\nInstall `Xcode command line tools`\n\n\nExample\n=========\n\n```javascript\nconst { XXHash32, XXHash3 } = require('xxhash-addon');\n\n// Hash a string using the static one-shot method.\nconst salute = 'hello there';\nconst buf_salute = Buffer.from(salute);\nconsole.log(XXHash32.hash(buf_salute).toString('hex'));\n\n// Digest a byte-stream (hash a buffer piece by piece).\nconst hasher32 = new XXHash32(Buffer.from([0, 0, 0, 0]));\nhasher32.update(buf_salute.slice(0, 3));\nconsole.log(hasher32.digest().toString('hex'));\nhasher32.update(buf_salute.slice(3));\nconsole.log(hasher32.digest().toString('hex'));\n\n// Reset the hasher for another hashing.\nhasher32.reset();\n\n// Using secret for XXH3\n// Same constructor call syntax, but hasher switches to secret mode whenever\n// it gets a buffer of at least 136 bytes.\nconst hasher3 = new XXHash3(require('fs').readFileSync('package-lock.json'));\n```\n\n\nFAQ\n=========\n\n1. Why TypeScript?\n* Short answer: for much better performance and security.\n* Long answer:\nDynamic type check is so expensive that it can hurt performance. In the world with no TypeScript, the streaming `update()` method had to check whether the buffer passed to it was an actual Node's `Buffer`. Failing to detect Buffer type might cause `v8` to `CHECK` and crashed Node process. Such dynamic type check could degrade performance of `xxhash-addon` by 10-15% per my onw benchmark on a low-end Intel Mac mini (on Apple Silicon, the difference is neglectable though.)\n\nSo how does TypeScript (TS) help? Static type check.\n\nThere is still a theoretical catch. TS' type system is structural so in a corner case where you have a class that is structurally like `Buffer` and you pass an instance of that class to `update()`. This is an extreme case that should never happen in practice. Nevertheless, there are official techniques to 'force' nominal typing. Check https://www.typescriptlang.org/play#example/nominal-typing for an in-depth.\n\nIf you don't use TS then you probably want to enable run-time type check of `xxhash-addon`. Uncomment the line `# \"defines\": [ \"ENABLE_RUNTIME_TYPE_CHECK\" ]` in `binding.gyp` and re-compile the addon. Use this at your own risk.\n\n\nDevelopment\n=========\n\nThis is for people who are interested in creating a PR.\n\n**How to clone?**\n\n```bash\ngit clone https://github.com/ktrongnhan/xxhash-addon\ngit submodule update --init\nnpm run debug:build\nnpm run benchmark\nnpm test\n```\nNote: `debug:build` compiles and links with Address Sanitizer (`-fsanitze=address`). `npm test` may not work out-of-the-box on macOS.\n\n**How to debug asan build?**\n\nYou may have troubles running tests with asan build. Here is my snippet to get it running under `lldb` on macOS.\n\n```bash\n$ lldb node -- --test xxhash-addon.test.js\n(lldb) env DYLD_INSERT_LIBRARIES=/Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/lib/darwin/libclang_rt.asan_osx_dynamic.dylib\n(lldb) env ASAN_OPTIONS=detect_leaks=1\n(lldb) breakpoint set -f src/addon.c -l 100\n(lldb) run\n(lldb) next\n```\n\nOR\n\n```bash\nDYLD_INSERT_LIBRARIES=$(clang --print-file-name=libclang_rt.asan_osx_dynamic.dylib) ASAN_OPTIONS=detect_leaks=1 node --test xxhash-addon.test.js\n```\n\nKey takeaways:\n* If you see an error saying ASan Interceptor is loaded too late, set the env `DYLD_INSERT_LIBRARIES`. You need to use absolute path to your Node.js binary as well. Curious why? [An interesting article](https://jonasdevlieghere.com/sanitizing-python-modules).\n* ASan doesn't detect mem-leak on macOS by default. You may want to turn this on with the env `ASAN_OPTIONS=detect_leaks=1`.\n\nIf you are debugging on Linux with GCC as your default compiler, here is a helpful oneliner:\n\n```bash\n$ LD_PRELOAD=$(gcc -print-file-name=libasan.so) LSAN_OPTIONS=suppressions=suppr.lsan DEBUG=1 node --test xxhash-addon.test.js\n```\n\n**How to upgrade xxHash?**\n\nEverything should be set up already. Just pull from the release branch of xxHash.\n\n```bash\ngit submodule update --remote\ngit status\ngit add xxHash\ngit commit -m \"Bump xxHash to...\"\ngit push origin your_name/upgrade_deps\n```\n\n\n\n\nAPI reference\n===========\n\n### Streaming Interface\n```\nexport interface XXHash {\n  update(data: Buffer): void;\n  digest(): Buffer;\n  reset(): void;\n}\n```\n\n### XXHash32\n```\nexport class XXHash32 implements XXHash {\n  constructor(seed: Buffer); // Buffer must be 4-byte long.\n  update(data: Buffer): void;\n  digest(): Buffer;\n  reset(): void;\n  static hash(data: Buffer): Buffer; // One-shot with default seed (zero).\n}\n```\n\n### XXHash64\n```\nexport class XXHash64 implements XXHash {\n  constructor(seed: Buffer); // Buffer must be 4- or 8-byte long.\n  update(data: Buffer): void;\n  digest(): Buffer;\n  reset(): void;\n  static hash(data: Buffer): Buffer; // One-shot with default seed (zero).\n}\n```\n\n### XXHash3\n```\nexport class XXHash3 implements XXHash {\n  constructor(seed_or_secret: Buffer); // For using seed: Buffer must be 4- or 8-byte long; for using secret: must be at least 136-byte long.\n  update(data: Buffer): void;\n  digest(): Buffer;\n  reset(): void;\n  static hash(data: Buffer): Buffer; // One-shot with default seed (zero).\n}\n```\n\n### XXHash128\n```\nexport class XXHash128 implements XXHash {\n  constructor(seed_or_secret: Buffer); // For using seed: Buffer must be 4- or 8-byte long; for using secret: must be at least 136-byte long.\n  update(data: Buffer): void;\n  digest(): Buffer;\n  reset(): void;\n  static hash(data: Buffer): Buffer; // One-shot with default seed (zero).\n}\n```\n\n\n\nLicence\n===========\nThe project is licensed under BSD-2-Clause.\n","publish_time":1576993773307,"_cnpm_publish_time":1576993773307,"dist-tags":{"latest":"2.1.0"}}