Node.js N-API bindings for the LZ4 compression algorithm.
⚡ Fast · 🔒 Memory safe · 🧵 Uses libuv's thread pool
Using npm:
npm install lz4-napiUsing Yarn:
yarn add lz4-napiconst { readFile } = require('fs/promises')
const { compress } = require('lz4-napi')
// If you support top-level await:
const buffer = await readFile('./bigFile.dat')
const compressedBuffer = await compress(buffer)
// Store the compressed buffer somewhere.const { uncompress } = require('lz4-napi')
// If you support top-level await:
const compressedBuffer = await getFromSomeStorage()
const uncompressedBuffer = await uncompress(compressedBuffer)
// Do something with the uncompressed buffer.declare function compress(data: Buffer | string | ArrayBuffer | Uint8Array, dict?: string | Buffer): Promise<Buffer>declare function uncompress(data: Buffer | string | ArrayBuffer | Uint8Array, dict?: string | Buffer): Promise<Buffer>declare function compressFrame(
data: Buffer | string | ArrayBuffer | Uint8Array,
options?: {
contentChecksum?: boolean
blockChecksums?: boolean
},
) => Promise<Buffer>declare function decompressFrame(data: Buffer | string | ArrayBuffer | Uint8Array): Promise<Buffer>declare function compressSync(data: Buffer | string | ArrayBuffer | Uint8Array, dict?: string | Buffer): Bufferdeclare function uncompressSync(data: Buffer | string | ArrayBuffer | Uint8Array, dict?: string | Buffer): Bufferdeclare function compressFrameSync(
data: Buffer | string | ArrayBuffer | Uint8Array,
options?: {
contentChecksum?: boolean
blockChecksums?: boolean
},
) => BufferNote
Both options default to false, matching the existing behavior. Set contentChecksum to have decompressFrame or decompressFrameSync reject a corrupted frame instead of silently returning incorrect bytes.
declare function decompressFrameSync(data: Buffer | string | ArrayBuffer | Uint8Array): BufferThe benchmarks were run on the following hardware:
| Component | Specification |
|---|---|
| Processor | M4 Pro |
| CPU cores | 12 |
| Memory | 24 GB |
| Algorithm | Throughput | Margin | Comparison |
|---|---|---|---|
lz4 |
7,355 ops/s | ±1.73% | 0.39% slower |
lz4 dict |
6,375 ops/s | ±0.29% | 13.66% slower |
snappy |
7,384 ops/s | ±0.53% | Fastest |
gzip |
444 ops/s | ±0.50% | 93.99% slower |
deflate |
442 ops/s | ±0.62% | 94.01% slower |
brotli |
6 ops/s | ±0.73% | Slowest · 99.92% slower |
| Algorithm | Throughput | Margin | Comparison |
|---|---|---|---|
lz4 |
19,095 ops/s | ±1.51% | Fastest |
lz4 dict |
17,644 ops/s | ±1.51% | 7.6% slower |
snappy |
14,424 ops/s | ±0.50% | 24.46% slower |
gzip |
2,442 ops/s | ±0.60% | 87.21% slower |
deflate |
2,467 ops/s | ±0.61% | 87.08% slower |
brotli |
1,659 ops/s | ±0.43% | Slowest · 91.31% slower |
Each suite completed all six cases.
This project is intentionally simple and focused on my needs, but ideas and contributions are welcome.
This project uses Conventional Commits. Be sure to use the standard commit format, or your PR will not be accepted.
- Fork the project.
- Create your feature branch (
git checkout -b feature/AmazingFeature). - Commit your changes (
git commit -m 'feat(scope): some AmazingFeature'). - Push to the branch (
git push origin feature/AmazingFeature). - Open a pull request.
- Brooooooklyn/snappy — inspiration and project structure
Distributed under the MIT License. See LICENSE for more information.