Skip to main content

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

From v2.3

@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.

From v5.0

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:

NeedRecommended path
Normal application useImport the library-neutral class from the package root
Smallest possible initial bundleUse the root class; it imports no fallback until built-in support is unavailable
A specific backend or reproducible benchmarkImport an implementation-named class
Existing v4 codeMigrate 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.

FormatBuilt-in pathCompact or optional fallback
GZIPCompressionStream / DecompressionStreamcompress-utils
DEFLATECompressionStream / DecompressionStreamcompress-utils
Raw DEFLATEDecompressionStream when supportedcompress-utils
BrotliRuntime-dependentcompress-utils
ZstandardRuntime-dependentcompress-utils
Snappy, LZ4, bzip2, XZNot exposed by the stream APIsDefault 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.

FormatClassImport subpathDefault policy
UncompressedNoCompressorno-compressorPass-through
GZIPGZipCompressorgzip-compressorBuilt-in stream, then compress-utils
DEFLATEDeflateCompressordeflate-compressorBuilt-in stream, then compress-utils
BrotliBrotliCompressorbrotli-compressorBuilt-in stream, then lazy compress-utils
ZstandardZstdCompressorzstd-compressorBuilt-in stream, then lazy compress-utils
SnappySnappyCompressorsnappy-compressorcompress-utils
LZ4 frameLZ4Compressorlz4-compressorLazy compress-utils
bzip2BZip2Compressorbzip2-compressorLazy compress-utils
XZ/LZMAXZCompressorxz-compressorLazy compress-utils

Default decompressors​

FormatClassImport subpathDefault policy
UncompressedNoDecompressorno-decompressorPass-through
GZIPGZipDecompressorgzip-decompressorBuilt-in stream, then compress-utils
DEFLATEDeflateDecompressordeflate-decompressorBuilt-in stream, then compress-utils
BrotliBrotliDecompressorbrotli-decompressorBuilt-in stream, then compress-utils
ZstandardZstdDecompressorzstd-decompressorBuilt-in stream, then compress-utils
SnappySnappyDecompressorsnappy-decompressorcompress-utils
LZ4LZ4Decompressorlz4-decompressorcompress-utils with TypeScript Hadoop framing
bzip2BZip2Decompressorbzip2-decompressorLazy compress-utils
XZ/LZMAXZDecompressorxz-decompressorLazy compress-utils
LZO1XLzoDecompressorlzo-decompressorDecoder-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.

FormatClassImport subpathBest for
UncompressedNoCompressorno-compressorPass-through pipelines
GZIPGZipFflateCompressorgzip-compressor-fflateCompact synchronous JavaScript
GZIPGZipPakoCompressorgzip-compressor-pakoExisting Pako-based applications
GZIPGZipCompressUtilsCompressorgzip-compressor-compress-utilsIncremental operation
DEFLATEDeflateFflateCompressordeflate-compressor-fflateCompact synchronous JavaScript
DEFLATEDeflatePakoCompressordeflate-compressor-pakoExisting Pako-based applications
DEFLATEDeflateCompressUtilsCompressordeflate-compressor-compress-utilsIncremental operation
BrotliBrotliCompressUtilsCompressorbrotli-compressor-compress-utilsBrotli encoding
ZstandardZstdCompressUtilsCompressorzstd-compressor-compress-utilsZstandard encoding
SnappySnappyJSCompressorsnappy-compressor-snappyjsCompact JavaScript
SnappySnappyCompressUtilsCompressorsnappy-compressor-compress-utilsSnappy encoding
LZ4 frameLZ4JSCompressorlz4-compressor-lz4jsCompact JavaScript
LZ4 frameLZ4CompressUtilsCompressorlz4-compressor-compress-utilsLZ4 frame encoding
bzip2BZip2CompressUtilsCompressorbzip2-compressor-compress-utilsbzip2 encoding
XZ/LZMAXZCompressUtilsCompressorxz-compressor-compress-utilsXZ/LZMA encoding

Specific decompressor implementations​

These imports bypass the default selection policy and pin one backend.

FormatClassImport subpathBest for
UncompressedNoDecompressorno-decompressorPass-through pipelines
GZIPGZipFflateDecompressorgzip-decompressor-fflateCompact synchronous JavaScript
GZIPGZipPakoDecompressorgzip-decompressor-pakoExisting Pako-based applications
GZIPGZipCompressUtilsDecompressorgzip-decompressor-compress-utilsIncremental operation
DEFLATEDeflateFflateDecompressordeflate-decompressor-fflateCompact synchronous JavaScript
DEFLATEDeflatePakoDecompressordeflate-decompressor-pakoExisting Pako-based applications
DEFLATEDeflateCompressUtilsDecompressordeflate-decompressor-compress-utilsIncremental operation
BrotliBrotliShimDecompressorbrotli-decompressor-shimBundled JavaScript decoder shim
BrotliBrotliCompressUtilsDecompressorbrotli-decompressor-compress-utilsIncremental operation
ZstandardZstdFzstdDecompressorzstd-decompressor-fzstdCompact synchronous JavaScript
ZstandardZstdCompressUtilsDecompressorzstd-decompressor-compress-utilsIncremental operation
SnappySnappyJSDecompressorsnappy-decompressor-snappyjsCompact JavaScript
SnappySnappyHysnappyDecompressorsnappy-decompressor-hysnappySmall synchronous embedded-WASM decoder
SnappySnappyCompressUtilsDecompressorsnappy-decompressor-compress-utilsSnappy decoding
LZ4 frameLZ4JSDecompressorlz4-decompressor-lz4jsCompact JavaScript
LZ4 frameLZ4CompressUtilsDecompressorlz4-decompressor-compress-utilsLZ4 frame decoding
bzip2BZip2CompressUtilsDecompressorbzip2-decompressor-compress-utilsbzip2 decoding
XZ/LZMAXZCompressUtilsDecompressorxz-decompressor-compress-utilsXZ/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() and decompress() process one buffer asynchronously.
  • compressBatches() and decompressBatches() accept iterable or async iterable buffers.
  • compressSync() and decompressSync() are available where the selected implementation has a synchronous codec. Some compatibility codecs require await 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​

FormatTypical useDesign emphasis
GZIPHTTP content, files ending in .gzBroad compatibility
DEFLATEHTTP content and ZIP internalsCompact, established format
BrotliHTTP content and ParquetBetter density, slower encoding
SnappyParquet and data systemsVery fast operation
LZ4Arrow/Feather and data systemsVery fast operation
ZstandardArrow/Feather, Parquet, and data systemsStrong speed-to-density balance
bzip2Legacy archives and datasetsCompression density
XZ/LZMAArchives and datasetsHigh 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.

FormatImport 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