对于基于 zlib 的流
【For zlib-based streams】
来自 zlib/zconf.h,为 Node.js 使用而修改:
【From zlib/zconf.h, modified for Node.js usage:】
deflate 的内存要求是(以字节为单位):
【The memory requirements for deflate are (in bytes):】
(1 << (windowBits + 2)) + (1 << (memLevel + 9)) 也就是说:128K 用于 windowBits = 15 + 128K 用于 memLevel = 8(默认值),再加上几千字节用于小对象。
【That is: 128K for windowBits = 15 + 128K for memLevel = 8
(default values) plus a few kilobytes for small objects.】
例如,要将默认内存需求从 256K 降低到 128K,应将选项设置为:
【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.】
inflate 的内存需求(以字节为单位)为 1 << windowBits。也就是说,对于 windowBits = 15(默认值),需要 32K,再加上一些用于小对象的少量内存。
【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.】
这还包括一个大小为 chunkSize 的单一内部输出缓冲区,默认大小为16K。
【This is in addition to a single internal output slab buffer of size
chunkSize, which defaults to 16K.】
zlib 压缩的速度最显著地受 level 设置影响。较高的压缩级别会带来更好的压缩效果,但完成的时间会更长。较低的压缩级别会导致压缩效果较差,但速度会快得多。
【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.】
一般来说,较高的内存使用选项意味着 Node.js 调用 zlib 的次数会更少,因为它能够在每次 write 操作中处理更多的数据。因此,这是影响速度的另一个因素,但代价是增加了内存使用。
【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.】