snappy

August 12, 2026 ยท View on GitHub

https://github.com/Brooooooklyn/snappy/actions Install size

!!! For snappy@6.x and below, please go to node-snappy.

More background about the 6-7 changes, please read this, Thanks @kesla .

๐Ÿš€ Help me to become a full-time open-source developer by sponsoring me on Github

Fastest Snappy compression library in Node.js, powered by napi-rs and rust-snappy.

For small size data, snappyjs is faster, and it support browser. But it doesn't have async API, which is important for Node.js program.

Install this package

yarn add snappy

Support matrix

snappy support matrix. Node.js >= 10; CI tests Node 22 and 24. 18 prebuilt targets across Windows, macOS, Linux, Android, FreeBSD, OpenHarmony and wasm32-wasi: 11 CI-tested, 7 built but untested.
Full matrix as text

Node.js

engines.node is >= 10. CI tests Node 22 and Node 24.

Targets

Rust triplePlatformCI
x86_64-pc-windows-msvcWindows x64tested โ€” node 22, 24
aarch64-pc-windows-msvcWindows arm64tested โ€” node 22, 24
i686-pc-windows-msvcWindows x32built, not tested
x86_64-apple-darwinmacOS x64tested โ€” node 22, 24
aarch64-apple-darwinmacOS arm64tested โ€” node 22, 24
x86_64-unknown-linux-gnuLinux x64 gnutested โ€” node 22, 24
x86_64-unknown-linux-muslLinux x64 musltested โ€” node 22, 24
aarch64-unknown-linux-gnuLinux arm64 gnutested โ€” node 22, 24
aarch64-unknown-linux-muslLinux arm64 musltested โ€” node 22, 24
armv7-unknown-linux-gnueabihfLinux armv7 gnutested โ€” node 22 only
s390x-unknown-linux-gnuLinux s390xtested โ€” node 22, 24
x86_64-unknown-freebsdFreeBSD x64built, not tested
powerpc64le-unknown-linux-gnuLinux ppc64lebuilt, not tested
riscv64gc-unknown-linux-gnuLinux riscv64built, not tested
aarch64-linux-androidAndroid arm64built, not tested
arm-linux-androideabiAndroid armv7built, not tested
aarch64-unknown-linux-ohosOpenHarmony arm64built, not tested
wasm32-wasip1-threadswasm32-wasi, browsertested โ€” node 24 (NAPI_RS_FORCE_WASI)

Eighteen targets: eleven CI-tested, seven built but not exercised.

Browser

Bundlers resolve the wasm package through the browser export condition. The wasm build allocates shared memory and spawns worker threads, so SharedArrayBuffer must be available โ€” the page has to be cross-origin isolated, served with Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp.

API

One-shot (raw Snappy block format)

export function compressSync(input: Buffer | string | ArrayBuffer | Uint8Array): Buffer
export function compress(input: Buffer | string | ArrayBuffer | Uint8Array): Promise<Buffer>
export function uncompressSync(compressed: Buffer): Buffer
export function uncompress(compressed: Buffer): Promise<Buffer>

Streaming (framed Snappy format)

Streaming uses the Snappy frame format (file extension .sz). This is not the same wire format as the one-shot APIs above โ€” framed output cannot be passed to uncompress(), and raw blocks cannot be passed to the stream decompressors.

Incremental classes

import { Compressor, Decompressor } from 'snappy'

const compressor = new Compressor()
const parts = [compressor.update('Hello '), compressor.update('snappy ๐Ÿš€'), await compressor.finish()]
const compressed = Buffer.concat(parts)

const decompressor = new Decompressor()
const restored = Buffer.concat([decompressor.update(compressed), await decompressor.finish()])
console.log(restored.toString('utf8')) // Hello snappy ๐Ÿš€

The valid stream is the concatenation of every update() output plus the finish() tail.

Web Streams

import { compressStream, uncompressStream } from 'snappy'

const restored = uncompressStream(compressStream(source)) // ReadableStream<Uint8Array>

input must be a WHATWG ReadableStream; wrap a Node Readable with Readable.toWeb().

On wasm / browser builds the native transforms are unavailable; a buffered class-API polyfill is used automatically.

Node Duplex factories

import { createReadStream, createWriteStream } from 'node:fs'
import { createCompressStream, createUncompressStream } from 'snappy'

createReadStream('input.txt').pipe(createCompressStream()).pipe(createWriteStream('input.txt.sz'))

Requires a modern Node.js with Web Streams and Duplex.fromWeb (effectively Node 18+).

Performance

Hardware

OS: Windows 11 x86_64
Host: Micro-Star International Co., Ltd. MS-7C35
Kernel: 10.0.22000
Terminal: Windows Terminal
CPU: AMD Ryzen 9 5950X (32) @ 3.400GHz
Memory: 32688MiB

Result

Running "Compress" suite...
Progress: 100%

  snappy:
    4 220 ops/s, ยฑ0.66%   | fastest

  snappy-v6:
    2 018 ops/s, ยฑ0.84%   | 52.18% slower

  gzip:
    233 ops/s, ยฑ0.52%     | slowest, 94.48% slower

  deflate:
    235 ops/s, ยฑ0.45%     | 94.43% slower

  brotli:
    7 ops/s, ยฑ0.51%       | slowest, 99.85% slower

Finished 4 cases!
  Fastest: snappy
  Slowest: brotli

Running "Decompress" suite...
Progress: 100%

  snappy:
    8 528 ops/s, ยฑ1.03%   | fastest

  snappy-v6:
    6 357 ops/s, ยฑ1.76%   | 25.46% slower

  gzip:
    1 406 ops/s, ยฑ1.80%   | slowest, 83.51% slower

  deflate:
    1 435 ops/s, ยฑ1.88%   | 83.17% slower

  brotli:
    1 208 ops/s, ยฑ1.50%   | slowest, 86.99% slower

Finished 4 cases!
  Fastest: snappy
  Slowest: brotli