线程池使用和性能注意事项


¥Threadpool usage and performance considerations

所有 zlib API,除了那些显式同步的 API,都使用 Node.js 内部线程池。这可能会在某些应用中导致令人惊讶的效果和性能限制。

¥All zlib APIs, except those that are explicitly synchronous, use the Node.js internal threadpool. This can lead to surprising effects and performance limitations in some applications.

同时创建和使用大量压缩对象会导致显着的内存碎片。

¥Creating and using a large number of zlib objects simultaneously can cause significant memory fragmentation.

import zlib from 'node:zlib';
import { Buffer } from 'node:buffer';

const payload = Buffer.from('This is some data');

// WARNING: DO NOT DO THIS!
for (let i = 0; i < 30000; ++i) {
  zlib.deflate(payload, (err, buffer) => {});
}const zlib = require('node:zlib');

const payload = Buffer.from('This is some data');

// WARNING: DO NOT DO THIS!
for (let i = 0; i < 30000; ++i) {
  zlib.deflate(payload, (err, buffer) => {});
}

在前面的示例中,同时创建了 30,000 个 deflate 实例。由于某些操作系统如何处理内存分配和释放,这可能会导致严重的内存碎片。

¥In the preceding example, 30,000 deflate instances are created concurrently. Because of how some operating systems handle memory allocation and deallocation, this may lead to significant memory fragmentation.

强烈建议缓存压缩操作的结果以避免重复工作。

¥It is strongly recommended that the results of compression operations be cached to avoid duplication of effort.