对于基于 zlib 的流


来自 zlib/zconf.h,修改为 node.js 的用法:

解压所需的内存是(字节为单位):

(1 << (windowBits + 2)) + (1 << (memLevel + 9))

就是: 当设置为 windowBits = 15 和 memLevel = 8 时(默认值),小的对象需要 128K 加上几千字节。

例如,为了将默认内存需求 256K 减少到 128K,应该这样设置:

const options = { windowBits: 14, memLevel: 7 };

这能实现,然而,通常会降低压缩水平。

压缩所需的内存是 1 << windowBits(字节为单位)。 既是, 设置为 windowBits = 15(默认值)时,小的对象需要 32K 加上几千字节。

这是一个大小为 chunkSize 单个内部输出 slab 缓冲,默认为 16K。

level 的设置是影响 zlib 压缩速度最大因素。 更高的等级设置会得到更高的压缩水平,然而需要更长的时间完成。 较低的等级设置会导致较少的压缩,但会大大加快速度。

通常来说, 更大的内存使用选项意味着 Node.js 必须减少调用 zlib,因为它的每个 write 操作能够处理更多的数据。 所以,这是另外一个影响速度的因素,代价是内存的占用。

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.