compress-utils
WASM compression bindings for zstd, brotli, zlib, bz2, lz4, xz, snappy, gzip - all tree-shakeable.
Last updated 14 days ago by GitHub Actions .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install compress-utils 
SYNC missed versions from official npm registry.

compress-utils

npm version npm downloads WASM build License: MIT

One portable API for Zstandard, Brotli, zlib, gzip, bzip2, LZ4, XZ/LZMA and Snappy, plus raw DEFLATE, LZ4 blocks and LZO decoding, in every JavaScript runtime, with decompression ahead of or on par with every other JavaScript and WASM library we tested. Tree-shakeable per-algorithm imports with TypeScript types included, all backed by one C core compiled to WebAssembly.

Installation

npm install compress-utils
# or: pnpm add / yarn add / bun add / deno add npm:compress-utils

ESM only, with TypeScript types for every subpath and no system dependencies. Tested in CI on Node 20, 22 and 24, Bun, Deno 2.x and Chromium, Firefox and WebKit; it also loads from a CDN such as esm.sh (CDN usage).

Quick start

Each algorithm is its own subpath import. Pick the one you need, and the rest stay out of your bundle.

import { compress, decompress } from "compress-utils/zstd";

const input = new TextEncoder().encode("the quick brown fox ".repeat(50));

const compressed = await compress(input, { level: 9 });
const restored   = await decompress(compressed);

Available subpaths: compress-utils/zstd, /brotli, /zlib, /bz2, /lz4, /xz, /snappy, /gzip, plus the raw block formats /deflate, /lz4_raw and /lzo/decompress. The surface is identical across all of them. Each also has /<algo>/decompress and /<algo>/compress variants that ship only one direction for a much smaller .wasm; see bundle size.

Performance

<picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/dupontcyborg/compress-utils/main/docs/benchmarks/img/readme-wasm-dark.png"> Compression ratio against throughput in MB/s on a log axis for compress-utils for JavaScript and brotli-wasm, fflate, fzstd, lz4js, pako, snappyjs and zstd-wasm, one row per algorithm (zstd, brotli, zlib, xz, lz4, snappy, bz2) with compress on the left and decompress on the right, dickens one-shot on AWS c8g.xlarge (Graviton4, arm64), 2026-10-29 </picture>

compress-utils for JavaScript against other JavaScript compression libraries: speed against compression ratio.

Measured on AWS c8g.xlarge (Graviton4), dickens (10.2 MB of text), one-shot, 2026-10-29.

Full results here.

Bundle size

Gzipped size in KiB of each algorithm's .wasm module (gzip -9, measured on the build of 2026-10-29): the default import ships both directions, while the /compress and /decompress subpaths ship only one.

Algorithm Compress only Decompress only Both
zstd 100.2 38.3 124.3
brotli 152.1 85.0 168.8
zlib 35.5 31.5 45.1
gzip 35.2 31.4 44.9
xz 52.2 47.0 64.8
lz4 34.2 20.3 43.4
bz2 25.5 23.1 35.7
snappy 12.2 15.1 17.2

Each algorithm is a separate module and unused code is tree-shaken, so a page pays only for the algorithms and directions it imports. How the modules were made smaller: wasm-size.md.

Supported algorithms

Algorithm Subpath Wire format
Zstandard compress-utils/zstd ZSTD frame with content size
Brotli compress-utils/brotli Raw Brotli stream
zlib compress-utils/zlib zlib wrapper (RFC 1950)
gzip compress-utils/gzip gzip stream (RFC 1952)
bzip2 compress-utils/bz2 bzip2 stream
LZ4 compress-utils/lz4 LZ4 frame (interoperable with the lz4 CLI and .lz4 files)
XZ / LZMA compress-utils/xz XZ stream with CRC64
Snappy compress-utils/snappy Raw Snappy block (interoperable with reference snappy and python-snappy)
DEFLATE compress-utils/deflate Raw DEFLATE (RFC 1951), no wrapper
LZ4 block compress-utils/lz4_raw Raw LZ4 block, no frame or size prefix (one-shot compression, incremental decompression)
LZO1X compress-utils/lzo/decompress Raw LZO1X block (decompression only)

Imports are independent: import "compress-utils/zstd" and import "compress-utils/brotli" pull in two separate .wasm modules, not a combined bundle. Files are marked "sideEffects": false so unused exports are tree-shaken aggressively. For per-module .wasm sizes, and the smaller decode-only and encode-only builds, see bundle size.

Raw blocks and output capacity

Raw block formats such as those inside Parquet and ORC pages don't record their decompressed size, so pass expectedSize, an upper bound on the output; the result holds only the bytes actually decoded. More in the advanced guide.

import { decompress } from "compress-utils/lz4_raw";

const page = await decompress(block, { expectedSize: uncompressedPageSize });

Streaming

Web Streams

import { compressionStream, decompressionStream } from "compress-utils/zstd";

await fetch("/big-file.json")
    .then(r => r.body!.pipeThrough(compressionStream({ level: 3 })))
    .then(s => s.pipeTo(uploadDestination));

Manual streaming (with using for auto-cleanup)

import { createCompressStream } from "compress-utils/brotli";

using cs = await createCompressStream({ level: 5 });
const chunks: Uint8Array[] = [];
for (const chunk of input) chunks.push(cs.write(chunk));
chunks.push(cs.finish());
// cs is destroyed automatically on scope exit (TS 5.2+ / Node 22+).

Without using, call cs.destroy() explicitly. To stop at the end of one frame and get the bytes after it, create the decoder with { singleStream: true }; see stopping at the end of a stream.

Compression levels

Every algorithm accepts a level from 1 (fastest) to 10 (smallest). The binding maps that to each algorithm's native range internally, so you don't need to know that Zstandard goes 1–22 and zlib goes 1–9. The default is 5.

await compress(input, { level: 1 });   // fastest
await compress(input, { level: 10 });  // smallest

Errors and limits

Decompression is bounded by default: output is capped at 1 GiB, so a hostile or corrupt input cannot exhaust memory. Change the cap with setMaxDecompressedSize, and see the untrusted input guidance in the project README for what else to set when you decompress data you don't control.

Every failure throws a CompressError, which carries the underlying C status code and the algorithm name:

import { compress, decompress, CompressError } from "compress-utils/zstd";

try {
    await decompress(garbage);
} catch (e) {
    if (e instanceof CompressError) console.error(e.algorithm, e.code, e.message);
}

await version() returns the library version (for example "0.12.1"), and await setMaxDecompressedSize(bytes) caps decompress for every algorithm (default 1 GiB; 0 = unbounded). Streams have no limit unless you pass one: createDecompressStream({ maxOutput }) and decompressionStream({ maxOutput }) throw a CompressError once the output passes it.

Concurrency

Each WASM module instance runs on the thread that loaded it, so nothing is shared between workers: every Worker initializes its own instance and keeps its own streams. Synchronous calls block the calling thread, so run expensive ones in a Worker; see preloading and synchronous calls.

License

MIT. See LICENSE.


Part of compress-utils. Built by Nico Dupont.

Current Tags

  • 0.12.1                                ...           latest (14 days ago)

11 Versions

  • 0.12.1                                ...           14 days ago
  • 0.12.0                                ...           a month ago
  • 0.11.2                                ...           a month ago
  • 0.11.1                                ...           a month ago
  • 0.11.0                                ...           a month ago
  • 0.10.1                                ...           a month ago
  • 0.9.0                                ...           a month ago
  • 0.8.0                                ...           4 months ago
  • 0.7.1                                ...           6 months ago
  • 0.7.0                                ...           6 months ago
  • 0.7.0-rc.1                                ...           6 months ago
Maintainers (1)
Downloads
Total 13
Today 0
This Week 0
This Month 10
Last Day 0
Last Week 10
Last Month 3
Dependencies (0)
None
Dev Dependencies (7)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE