Node.js Reference ยท crypto
Node.js zlib.zstdCompress() method
Reference for zlib.zstdCompress(), zstdDecompress(), their sync forms and createZstdCompress() in Node.js: Zstandard parameters, levels, errors and version notes.
Zstandard (zstd) is a compression format designed at Facebook that offers gzip-like ratios at much higher speed, or Brotli-like ratios at a better speed than Brotli's top levels. Node.js supports it in node:zlib with zstdCompress(), zstdDecompress(), zstdCompressSync(), zstdDecompressSync() and the stream factories createZstdCompress() and createZstdDecompress().
The Zstandard support is newer than gzip, deflate and Brotli. It arrived in Node.js 22.15 and 23.8 and is still marked experimental in Node 24, so the API surface may still change and Node may print an experimental warning the first time you use it. Check your Node version before depending on it.
import { zstdCompressSync, zstdDecompressSync } from 'node:zlib'; const packed = zstdCompressSync('hello hello hello'); console.log(zstdDecompressSync(packed).toString());
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
zlib.zstdCompress(buffer, options?, callback: (err, result: Buffer) => void): void zlib.zstdDecompress(buffer, options?, callback): void zlib.zstdCompressSync(buffer, options?): Buffer zlib.zstdDecompressSync(buffer, options?): Buffer zlib.createZstdCompress(options?): ZstdCompress zlib.createZstdDecompress(options?): ZstdDecompress
| Name | Description |
|---|---|
buffer |
A string, Buffer, typed array, DataView or ArrayBuffer. |
options.params |
An object mapping zlib.constants.ZSTD_c_* (compression) or ZSTD_d_* (decompression) numbers to values. |
options.maxOutputLength |
Maximum output size in bytes for the buffer helpers. Exceeding it throws ERR_BUFFER_TOO_LARGE. |
options.chunkSize |
Size of internal output chunks. |
options.pledgedSrcSize |
The exact input size, if known up front, so the frame header can record it. |
Useful params keys: ZSTD_c_compressionLevel (negative "fast" levels up to 22; the default is 3), ZSTD_c_windowLog, ZSTD_c_checksumFlag (add a content checksum, 1 to enable) and ZSTD_d_windowLogMax (limit the decoder's memory use).
Returns: a Buffer for the sync forms, nothing for the callback forms, a Transform stream for the create* factories.
Errors: corrupt data fails with a code such as ZSTD_error_prefix_unknown; exceeding maxOutputLength gives ERR_BUFFER_TOO_LARGE; an unknown or out-of-range parameter gives ERR_ZSTD_INVALID_PARAM or a type error.
Usage notes
- The default level 3 is fast and already near gzip's ratio. Levels above 19 are "ultra" levels that use a lot of memory and time and rarely beat Brotli by much.
- Decompression is very fast at every level, usually faster than gzip.
- Browsers support
Content-Encoding: zstdin recent versions, so it is a valid choice for HTTP bodies, but check your audience before dropping gzip as a fallback. - Zstd frames start with the magic number
28 b5 2f fd, which is handy for sniffing the format. - A content checksum is off by default. Turn it on with
ZSTD_c_checksumFlagwhen you store frames and want corruption detected at decode time. - Use
maxOutputLengthfor untrusted input; see Decompression bombs.