Memory usage tuning
History
From zlib/zconf.h, modified for Node.js usage:
The memory requirements for deflate are (in bytes):
(1 << (windowBits + 2)) + (1 << (memLevel + 9));
That is: 128K for windowBits = 15 + 128K for memLevel = 8
(default values) plus a few kilobytes for small objects.
For example, to reduce the default memory requirements from 256K to 128K, the options should be set to:
const options = { windowBits: 14, memLevel: 7 };
This will, however, generally degrade compression.
The memory requirements for inflate are (in bytes) 1 << windowBits.
That is, 32K for windowBits = 15 (default value) plus a few kilobytes
for small objects.
This is in addition to a single internal output slab buffer of size
chunkSize, which defaults to 16K.
The speed of zlib compression is affected most dramatically by the
level setting. A higher level will result in better compression, but
will take longer to complete. A lower level will result in less
compression, but will be much faster.
In general, greater memory usage options will mean that Node.js has to make
fewer calls to zlib because it will be able to process more data on
each write operation. So, this is another factor that affects the
speed, at the cost of memory usage.
There are equivalents to the zlib options for Brotli-based streams, although these options have different ranges than the zlib ones:
- zlib's
leveloption matches Brotli'sBROTLI_PARAM_QUALITYoption. - zlib's
windowBitsoption matches Brotli'sBROTLI_PARAM_LGWINoption.
See below for more details on Brotli-specific options.
There are equivalents to the zlib options for Zstd-based streams, although these options have different ranges than the zlib ones:
- zlib's
leveloption matches Zstd'sZSTD_c_compressionLeveloption. - zlib's
windowBitsoption matches Zstd'sZSTD_c_windowLogoption.
See below for more details on Zstd-specific options.