Compression module
Choose the smallest codec that fits the runtime.
@loaders.gl/compression gives loaders, writers, and applications one API for lossless codecs in browsers and Node.js. Built-in streams are preferred when available; optional fallbacks stay out of the common bundle.
The codec path
Detect the runtime. Select the implementation. Move bytes.
The module keeps codec selection separate from the loader or writer that needs it. Applications can use stable root exports or choose an implementation explicitly for bundle and benchmark control.
Input
ArrayBuffer, typed bytes, or a stream-compatible payload
Selection
Native stream, compact fallback, or explicit backend
Execution
Main thread, worker, browser, or Node.js
Output
Compressed or decompressed binary data
@loaders.gl/compression provides lossless compression codecs with one consistent API in browsers
and Node.js. It is designed to keep the common path small: use a built-in codec when the runtime
has one, load compress-utils WASM when it does not, and load a larger or
specialized implementation only when an application asks for it.
Start here
Detailed guide
Codec selection and implementation details
The reference below covers installation, built-in paths, optional codecs, workers, implementation-specific imports, and compatibility guidance.
Choose an implementation based on what your application values most:
| Need | Recommended path |
|---|---|
| Normal application use | Import the library-neutral class from the package root |
| Smallest possible initial bundle | Use the root class; it imports no fallback until built-in support is unavailable |
| A specific backend or reproducible benchmark | Import an implementation-named class |
| Existing v4 code | Migrate root combined classes to direction-specific compressor/decompressor classes |
The live benchmarks compare the implementations in your browser, including throughput and focused bundle size.
Quick start
Import the default class for the direction you need. The default owns the built-in and fallback policy, so application imports stay stable as runtime support improves:
import {GZipDecompressor} from '@loaders.gl/compression/gzip';
const decompressor = new GZipDecompressor();
const data = await decompressor.decompress(compressedData);
Compressors and decompressors share stable base types, which makes codec selection easy to pass through loaders, writers, or application code:
import {GZipCompressor, GZipDecompressor} from '@loaders.gl/compression/gzip';
import {type Compressor, type Decompressor} from '@loaders.gl/compression/codec';
const compressors: Compressor[] = [new GZipCompressor()];
const decompressors: Decompressor[] = [new GZipDecompressor()];
Built-in codecs first
The root compressor and decompressor classes automatically select these paths. The lightweight
native-compression and native-decompression entrypoints expose the runtime's
CompressionStream and
DecompressionStream
implementations directly for applications that need lower-level control. They contain no fallback
codec imports. A helper returns null when the exact format is unavailable.
import {decompressWithNativeDecompressionStream} from '@loaders.gl/compression/native-decompression';
async function decompressGzip(input: ArrayBuffer): Promise<ArrayBuffer> {
const builtInOutput = await decompressWithNativeDecompressionStream(input, 'gzip');
if (builtInOutput) {
return builtInOutput;
}
const {GZipDecompressor} = await import('@loaders.gl/compression/gzip-decompressor');
return new GZipDecompressor({useNative: false}).decompress(input);
}
Browser support differs by codec. GZIP and DEFLATE are widely available; Brotli and Zstandard support is still runtime-dependent. See CompressionStream support, DecompressionStream support, and the Chromium Zstandard issue.
| Format | Built-in path | Compact or optional fallback |
|---|---|---|
| GZIP | CompressionStream / DecompressionStream | compress-utils |
| DEFLATE | CompressionStream / DecompressionStream | compress-utils |
| Raw DEFLATE | DecompressionStream when supported | compress-utils |
| Brotli | Runtime-dependent | compress-utils |
| Zstandard | Runtime-dependent | compress-utils |
| Snappy, LZ4, bzip2, XZ | Not exposed by the stream APIs | Default format implementation |
Default compressors
These classes are exported from @loaders.gl/compression. They are deliberately library-neutral,
keep fallback codecs behind dynamic imports, and optimize for balanced bundle size and runtime
performance. The listed subpaths provide direct access to the concrete default when synchronous
startup or explicit prebundling is required.
| Format | Class | Import subpath | Default policy |
|---|---|---|---|
| Uncompressed | NoCompressor | no-compressor | Pass-through |
| GZIP | GZipCompressor | gzip-compressor | Built-in stream, then compress-utils |
| DEFLATE | DeflateCompressor | deflate-compressor | Built-in stream, then compress-utils |
| Brotli | BrotliCompressor | brotli-compressor | Built-in stream, then lazy compress-utils |
| Zstandard | ZstdCompressor | zstd-compressor | Built-in stream, then lazy compress-utils |
| Snappy | SnappyCompressor | snappy-compressor | compress-utils |
| LZ4 frame | LZ4Compressor | lz4-compressor | Lazy compress-utils |
| bzip2 | BZip2Compressor | bzip2-compressor | Lazy compress-utils |
| XZ/LZMA | XZCompressor | xz-compressor | Lazy compress-utils |
Default decompressors
| Format | Class | Import subpath | Default policy |
|---|---|---|---|
| Uncompressed | NoDecompressor | no-decompressor | Pass-through |
| GZIP | GZipDecompressor | gzip-decompressor | Built-in stream, then compress-utils |
| DEFLATE | DeflateDecompressor | deflate-decompressor | Built-in stream, then compress-utils |
| Brotli | BrotliDecompressor | brotli-decompressor | Built-in stream, then compress-utils |
| Zstandard | ZstdDecompressor | zstd-decompressor | Built-in stream, then compress-utils |
| Snappy | SnappyDecompressor | snappy-decompressor | compress-utils |
| LZ4 | LZ4Decompressor | lz4-decompressor | compress-utils with TypeScript Hadoop framing |
| bzip2 | BZip2Decompressor | bzip2-decompressor | Lazy compress-utils |
| XZ/LZMA | XZDecompressor | xz-decompressor | Lazy compress-utils |
| LZO1X | LzoDecompressor | lzo-decompressor | Decoder-only compress-utils; output capacity required |
Specific compressor implementations
Most applications should use a default above. Choose a specific implementation only to pin a
backend, inject a different performance/size tradeoff, or run comparisons. Specific subpaths follow
FORMAT-DIRECTION-IMPLEMENTATION order—for example,
brotli-decompressor-compress-utils.
| Format | Class | Import subpath | Best for |
|---|---|---|---|
| Uncompressed | NoCompressor | no-compressor | Pass-through pipelines |
| GZIP | GZipFflateCompressor | gzip-compressor-fflate | Compact synchronous JavaScript |
| GZIP | GZipPakoCompressor | gzip-compressor-pako | Existing Pako-based applications |
| GZIP | GZipCompressUtilsCompressor | gzip-compressor-compress-utils | Incremental operation |
| DEFLATE | DeflateFflateCompressor | deflate-compressor-fflate | Compact synchronous JavaScript |
| DEFLATE | DeflatePakoCompressor | deflate-compressor-pako | Existing Pako-based applications |
| DEFLATE | DeflateCompressUtilsCompressor | deflate-compressor-compress-utils | Incremental operation |
| Brotli | BrotliCompressUtilsCompressor | brotli-compressor-compress-utils | Brotli encoding |
| Zstandard | ZstdCompressUtilsCompressor | zstd-compressor-compress-utils | Zstandard encoding |
| Snappy | SnappyJSCompressor | snappy-compressor-snappyjs | Compact JavaScript |
| Snappy | SnappyCompressUtilsCompressor | snappy-compressor-compress-utils | Snappy encoding |
| LZ4 frame | LZ4JSCompressor | lz4-compressor-lz4js | Compact JavaScript |
| LZ4 frame | LZ4CompressUtilsCompressor | lz4-compressor-compress-utils | LZ4 frame encoding |
| bzip2 | BZip2CompressUtilsCompressor | bzip2-compressor-compress-utils | bzip2 encoding |
| XZ/LZMA | XZCompressUtilsCompressor | xz-compressor-compress-utils | XZ/LZMA encoding |
Specific decompressor implementations
These imports bypass the default selection policy and pin one backend.
| Format | Class | Import subpath | Best for |
|---|---|---|---|
| Uncompressed | NoDecompressor | no-decompressor | Pass-through pipelines |
| GZIP | GZipFflateDecompressor | gzip-decompressor-fflate | Compact synchronous JavaScript |
| GZIP | GZipPakoDecompressor | gzip-decompressor-pako | Existing Pako-based applications |
| GZIP | GZipCompressUtilsDecompressor | gzip-decompressor-compress-utils | Incremental operation |
| DEFLATE | DeflateFflateDecompressor | deflate-decompressor-fflate | Compact synchronous JavaScript |
| DEFLATE | DeflatePakoDecompressor | deflate-decompressor-pako | Existing Pako-based applications |
| DEFLATE | DeflateCompressUtilsDecompressor | deflate-decompressor-compress-utils | Incremental operation |
| Brotli | BrotliShimDecompressor | brotli-decompressor-shim | Bundled JavaScript decoder shim |
| Brotli | BrotliCompressUtilsDecompressor | brotli-decompressor-compress-utils | Incremental operation |
| Zstandard | ZstdFzstdDecompressor | zstd-decompressor-fzstd | Compact synchronous JavaScript |
| Zstandard | ZstdCompressUtilsDecompressor | zstd-decompressor-compress-utils | Incremental operation |
| Snappy | SnappyJSDecompressor | snappy-decompressor-snappyjs | Compact JavaScript |
| Snappy | SnappyHysnappyDecompressor | snappy-decompressor-hysnappy | Small synchronous embedded-WASM decoder |
| Snappy | SnappyCompressUtilsDecompressor | snappy-decompressor-compress-utils | Snappy decoding |
| LZ4 frame | LZ4JSDecompressor | lz4-decompressor-lz4js | Compact JavaScript |
| LZ4 frame | LZ4CompressUtilsDecompressor | lz4-decompressor-compress-utils | LZ4 frame decoding |
| bzip2 | BZip2CompressUtilsDecompressor | bzip2-decompressor-compress-utils | bzip2 decoding |
| XZ/LZMA | XZCompressUtilsDecompressor | xz-decompressor-compress-utils | XZ/LZMA decoding |
Async, sync, and streaming
Prefer the async methods unless a synchronous call is required. Async methods can use built-in streams, initialize optional modules, and work with WASM-backed implementations.
compress()anddecompress()process one buffer asynchronously.compressBatches()anddecompressBatches()accept iterable or async iterable buffers.compressSync()anddecompressSync()are available where the selected implementation has a synchronous codec. Some compatibility codecs requireawait preload()before a sync call.- A codec with genuine streaming support emits output incrementally. The base classes otherwise concatenate the input and yield one result.
Built-in streams are not automatically the fastest choice for every workload. Creating a
CompressionStream or DecompressionStream and scheduling its asynchronous reads has a fixed
cost. That cost is usually amortized for large payloads and genuine streams, but it can dominate
formats such as Parquet that process many small, independently compressed blocks. For block-based
formats, benchmark a cached synchronous codec as well as the built-in stream path;
useNative: false selects compress-utils on the balanced GZIP, DEFLATE, Brotli, and Zstandard classes.
compress-utils is a required runtime dependency. The external reference backends (fzstd,
hysnappy, snappyjs, Pako, lz4js, zstd-codec, and the injectable brotli package) are
optional peer dependencies: package managers do not automatically install them for this package.
Default codecs still work without these packages. Install the corresponding package explicitly
before importing its backend-specific entry point, for example npm install pako for
@loaders.gl/compression/gzip-decompressor-pako. Injectable brotli and zstd-codec implementations also
require explicit installation and registration through modules; installing them alone does not
select them. The fflate implementation and Brotli decoder
shim are vendored and do not require an external package.
The deprecated zstd-compression entry point still uses fzstd for asynchronous fallback
when zstd-codec has not been injected. Install fzstd explicitly for that path, or migrate
to the default ZstdDecompressor, whose fallback uses the required compress-utils package.
compress-utils uses direction-specific algorithm subpaths so unrelated formats can be tree-shaken.
Lazy root classes and compatibility
The package root exports lightweight FormatCompressor and FormatDecompressor classes. Their
async methods call preload() automatically. preload() first selects a built-in implementation
when the runtime supports the format, otherwise it dynamically imports the concrete balanced
fallback. It also returns that concrete implementation for applications that want to cache or
inspect the selection explicitly.
Import a direct default or backend-specific subpath only when bundle composition must be pinned, or when a specific backend must be selected. WASM sync methods require preload.
The root exports only direction-specific codec classes. WASM fallbacks load dynamically when native support is unavailable; use an ESM bundler with code splitting to keep these modules out of the initial bundle. Explicit implementation subpaths intentionally include their codec. Legacy combined classes are available only through explicit subpaths during migration.
On Node.js 24 and later, compress-utils can use Node's source-phase WebAssembly
imports by starting Node with --conditions=compress-utils-wasm-source. Without
that condition it reads the WASM assets from the installed package. In browsers,
the bundler must emit the WASM assets alongside the codec modules. The codec
package currently resolves its own assets; task-managed WASM bytes or compiled
modules cannot be supplied to its public API.
See the v5 upgrade guide for import migrations and behavior changes.
Formats at a glance
| Format | Typical use | Design emphasis |
|---|---|---|
| GZIP | HTTP content, files ending in .gz | Broad compatibility |
| DEFLATE | HTTP content and ZIP internals | Compact, established format |
| Brotli | HTTP content and Parquet | Better density, slower encoding |
| Snappy | Parquet and data systems | Very fast operation |
| LZ4 | Arrow/Feather and data systems | Very fast operation |
| Zstandard | Arrow/Feather, Parquet, and data systems | Strong speed-to-density balance |
| bzip2 | Legacy archives and datasets | Compression density |
| XZ/LZMA | Archives and datasets | High compression density |
For measured comparisons rather than general rules, run the compression benchmarks on the target browser and hardware.
Internal Deflate engine
The GZIP, zlib, and raw DEFLATE JavaScript paths use an internal TypeScript
fork of fflate 0.7.4. Existing *Fflate* classes and subpaths keep their names
and options. The original fflate package is a development reference for parity
tests and benchmarks; these production paths do not import it. Native stream
selection and optional Pako adapters remain available.
See compression benchmarks for a live internal-versus-upstream comparison.
compress-utils and incremental raw decoding
All default fallback codecs use compress-utils 0.10.1, a runtime dependency loaded through format- and direction-specific dynamic imports. Root async methods still prefer built-in streams. Use ESM code splitting to keep WASM codec modules out of the initial bundle. Alternative JavaScript implementations remain available through explicit backend subpaths for reference and comparison.
Call await preload() before synchronous work. Direct default subpaths now require preload too;
async methods initialize automatically. Pass {useNative: false} to a root codec when preloading
for sync methods, since native browser streams only support async work.
LzoDecompressor decodes raw LZO1X blocks only. There is no LZO compressor. Pass the output capacity
as the second argument, or as compressUtils.expectedSize. The result contains the actual decoded
bytes, which may be fewer than the capacity.
import {LzoDecompressor} from '@loaders.gl/compression/lzo';
const decoder = new LzoDecompressor();
await decoder.preload();
const output = decoder.decompressSync(compressedBlock, outputCapacity);
Parquet supports LZO pages, including Hadoop block/chunk framing from parquet-java, through a TypeScript adapter. ORC supports LZO inside its existing three-byte chunk framing. Neither loader encodes LZO. Standalone lzop containers and other LZO variants are not supported.
For raw DEFLATE or LZ4, use {raw: true} with the matching compress-utils compressor or decoder.
Raw LZ4 decoding requires an output capacity. Raw formats share their codec WASM assets with the
wrapped formats.
Raw LZ4 (new LZ4Decompressor({raw: true})) and LZO support genuine incremental
decompressBatches() through compress-utils 0.10.1. They accept fragments of one raw block
and emit output during decoding without requiring output capacity. Root raw LZ4 atomic calls
and LZO atomic calls still require capacity. Root LZ4 batch decoding defaults to framed LZ4; use raw: true
for raw blocks. Legacy Hadoop framing remains available only through atomic decoding.
Use bounded input chunks. The decoder retains a fixed history plus the current undrained chunk;
maxOutputByteLength limits cumulative emitted bytes (512 MiB by default). Output is provisional
until the iterator completes and validates the block ending. Stopping iteration releases the
native stream. LZO encoding and raw LZ4 incremental encoding are not supported.
Sources and credits
See the package source and license inventory and the project-wide Attributions and Credits.
Package exports
All exported subpaths (51)
These are the package's public exports, including format entry points, parsers, writers, sources, and worker assets.
| Format | Import subpath |
|---|---|
@loaders.gl/compression/*-compressor-compress-utils | |
@loaders.gl/compression/*-compressor-fflate | |
@loaders.gl/compression/*-compressor-pako | |
@loaders.gl/compression/*-decompressor-compress-utils | |
@loaders.gl/compression/*-decompressor-fflate | |
@loaders.gl/compression/*-decompressor-pako | |
@loaders.gl/compression/brotli | |
@loaders.gl/compression/brotli-compressor | |
@loaders.gl/compression/brotli-decode | |
@loaders.gl/compression/brotli-decompressor | |
@loaders.gl/compression/brotli-decompressor-shim | |
@loaders.gl/compression/bzip2 | |
@loaders.gl/compression/bzip2-compressor | |
@loaders.gl/compression/bzip2-decompressor | |
@loaders.gl/compression/codec | |
@loaders.gl/compression/compression-worker-node.js | |
@loaders.gl/compression/compression-worker.js | |
@loaders.gl/compression/deflate | |
@loaders.gl/compression/deflate-compressor | |
@loaders.gl/compression/deflate-decompressor | |
@loaders.gl/compression/gzip | |
@loaders.gl/compression/gzip-compressor | |
@loaders.gl/compression/gzip-decompressor | |
@loaders.gl/compression/lz4 | |
@loaders.gl/compression/lz4-compressor | |
@loaders.gl/compression/lz4-compressor-lz4js | |
@loaders.gl/compression/lz4-decompressor | |
@loaders.gl/compression/lz4-decompressor-lz4js | |
@loaders.gl/compression/lzo | |
@loaders.gl/compression/lzo-decompressor | |
@loaders.gl/compression/lzo-decompressor-compress-utils | |
@loaders.gl/compression/native-compression | |
@loaders.gl/compression/native-decompression | |
@loaders.gl/compression/no-compressor | |
@loaders.gl/compression/no-decompressor | |
@loaders.gl/compression/none | |
@loaders.gl/compression/snappy | |
@loaders.gl/compression/snappy-compressor | |
@loaders.gl/compression/snappy-compressor-snappyjs | |
@loaders.gl/compression/snappy-decompressor | |
@loaders.gl/compression/snappy-decompressor-hysnappy | |
@loaders.gl/compression/snappy-decompressor-snappyjs | |
@loaders.gl/compression/worker | |
@loaders.gl/compression/xz | |
@loaders.gl/compression/xz-compressor | |
@loaders.gl/compression/xz-decompressor | |
@loaders.gl/compression/zstd | |
@loaders.gl/compression/zstd-compressor | |
@loaders.gl/compression/zstd-decompressor | |
@loaders.gl/compression/zstd-decompressor-fzstd | |
@loaders.gl/compression/zstd-fallback |