blog

Express compress

Today I discovered a nifty little npm package called compression. Per the docs, a one-line change applies Brotli compression to all API responses:

const app = express()

// compress all responses
app.use(compression())

(compression() accepts an options object so you can switch between gzip/Brotli, compression level, etc.)

While it’s still early days and I didn’t actually ship to production, initial tests look very promising:

before:

after:

Compressing and caching all static assets on the client was my baseline for any new web project, but I never thought of doing the same thing for the API. Huge miss.

One big caveat is that compression eats CPU and memory resources. For a high-traffic API it can put the bill through the roof. In such situations, offloading compression to a load balancer (assuming you have one if running an API at scale) is a better choice. But when starting small, this feels like a good starting point.