Optimize compression with syncThreshold
mainThe syncThreshold option controls when responses are compressed synchronously versus via a streaming path.
- Synchronous Path: Used for payloads (Strings or Buffers) smaller than
syncThreshold. This is faster for small responses as it avoids the overhead of a zlib stream and uses less memory. Synchronous compression also allows for an accurateContent-Lengthheader. - Streaming Path: Used for larger payloads, streams, Web
ReadableStreams, and FetchResponses. This offloads work to libuv's threadpool to avoid blocking the event loop.
Note for CPU-constrained environments: In containers with limited CPU quotas (e.g., Kubernetes cpu: 500m), Node.js may incorrectly report a high number of cores. In these cases, you should explicitly set a higher syncThreshold to favor synchronous compression.
await fastify.register(
import('@fastify/compress'),
{ syncThreshold: 65536 }
)