Skip to content

Repository files navigation

⚡ lz4-napi

Node.js N-API bindings for the LZ4 compression algorithm.

Powered by Rust, napi-rs, and lz4-flex.

⚡ Fast · 🔒 Memory safe · 🧵 Uses libuv's thread pool


Table of contents

Installation

Using npm:

npm install lz4-napi

Using Yarn:

yarn add lz4-napi

Usage

Compress data

const { 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.

Uncompress data

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.

API reference

Promise API

compress

declare function compress(data: Buffer | string | ArrayBuffer | Uint8Array, dict?: string | Buffer): Promise<Buffer>

uncompress

declare function uncompress(data: Buffer | string | ArrayBuffer | Uint8Array, dict?: string | Buffer): Promise<Buffer>

compressFrame

declare function compressFrame(
  data: Buffer | string | ArrayBuffer | Uint8Array,
  options?: {
    contentChecksum?: boolean
    blockChecksums?: boolean
  },
) => Promise<Buffer>

decompressFrame

declare function decompressFrame(data: Buffer | string | ArrayBuffer | Uint8Array): Promise<Buffer>

Synchronous API

compressSync

declare function compressSync(data: Buffer | string | ArrayBuffer | Uint8Array, dict?: string | Buffer): Buffer

uncompressSync

declare function uncompressSync(data: Buffer | string | ArrayBuffer | Uint8Array, dict?: string | Buffer): Buffer

compressFrameSync

declare function compressFrameSync(
  data: Buffer | string | ArrayBuffer | Uint8Array,
  options?: {
    contentChecksum?: boolean
    blockChecksums?: boolean
  },
) => Buffer

Note

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.

decompressFrameSync

declare function decompressFrameSync(data: Buffer | string | ArrayBuffer | Uint8Array): Buffer

Performance

Hardware

The benchmarks were run on the following hardware:

Component Specification
Processor M4 Pro
CPU cores 12
Memory 24 GB

Benchmark

Compression

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

Decompression

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.

Contributing

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.

  1. Fork the project.
  2. Create your feature branch (git checkout -b feature/AmazingFeature).
  3. Commit your changes (git commit -m 'feat(scope): some AmazingFeature').
  4. Push to the branch (git push origin feature/AmazingFeature).
  5. Open a pull request.

Acknowledgments

License

Distributed under the MIT License. See LICENSE for more information.

About

Fastest lz4 compression library in Node.js, powered by napi-rs and lz4-flex.

Topics

Resources

Stars

73 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages