tezvyn:

Node's zlib Module: Trading CPU for Bandwidth

AI-drafted, machine-checkedSource: nodejs.orgadvanced

Node's `zlib` module trades CPU cycles for network bandwidth by shrinking data with algorithms like Gzip and Brotli. Use it to compress large API responses or files before sending them. The main footgun: never use synchronous `...Sync` methods in a server.

WHY IT EXISTS: Transmitting large amounts of data over a network is slow and expensive. The zlib module was created to provide a native, efficient way to reduce the size of data payloads directly within a Node.js application, improving performance by saving bandwidth at the cost of CPU cycles.

THE MENTAL MODEL: Think of zlib as a set of tools for shrinking and unshrinking data. You hand it a large buffer, like a big JSON string, and it gives you back a much smaller buffer. This smaller buffer can be sent over the network faster. The recipient then uses the corresponding "unshrinking" tool to restore the original data. The trade-off is clear: you spend CPU time on both the sender's and receiver's end to save time on the wire.

HOW IT WORKS: The zlib module exposes several compression libraries, including Gzip, Deflate, Brotli, and Zstandard (Zstd). It provides three primary interaction patterns. First, synchronous methods like gzipSync(), which are simple but block the Node.js event loop. Second, asynchronous, callback-based methods like gzip(), which offload work to a thread pool and are non-blocking. Third, stream-based APIs like createGzip(), which are the most memory-efficient and ideal for processing large data without loading it all into memory at once.

WHEN TO USE IT: Use zlib when you need to reduce the size of text-based data being sent over a network or written to disk. Its primary use case in Node.js is compressing HTTP response bodies. A web server can check the client's Accept-Encoding header and, if it supports gzip or br (Brotli), pipe the response through a compression stream.

WHEN NOT TO USE IT: Avoid using compression on data that is already compressed, such as JPEG images, MP4 videos, or ZIP archives. Attempting to re-compress this data will waste CPU cycles and may even slightly increase the file size. Most importantly, never use the synchronous methods (...Sync) in any concurrent environment, like a web server, as they will block the event loop and severely degrade performance.

ONE CANONICAL EXAMPLE: A common pattern in an Express.js server is to use middleware that handles compression automatically. Under the hood, it inspects the Accept-Encoding request header. If a browser sends Accept-Encoding: gzip, br, the middleware will pipe the outgoing response stream through zlib.createGzip() or zlib.createBrotliCompress() before sending it, setting the Content-Encoding response header accordingly. The browser then automatically decompresses the content.

Read the original → nodejs.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.