Compression
zlib.brotliCompress()
Compresses bytes using Brotli asynchronously. Tune quality using measured workload data; higher quality can cost more CPU. Tested on Node.js 22.16.0. Example output (one line per entry): ["book"].
Formal Syntax & Structure
nodejs syntaxnodejs
zlib.brotliCompress(buffer[, options], callback)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| buffer | Buffer | string | TypedArray | DataView | ArrayBuffer | Input. |
| options | object | Brotli parameters. |
| callback | Function | Completion callback. |
Return Value
undefined
Compressed Buffer via callback.
Working Example
Example Snippetnodejs
import { brotliCompress, brotliDecompressSync, constants } from 'node:zlib';import { promisify } from 'node:util';const packed = await promisify(brotliCompress)('book', { params: { [constants.BROTLI_PARAM_QUALITY]: 4 } });console.log(brotliDecompressSync(packed).toString());