JavaScript Compression
Compression algorithms reduce the number of bytes transferred over the network without changing application semantics. Frontend build pipelines commonly compress JavaScript with Brotli or Gzip before delivery to reduce transfer size and improve load performance.
JavaScript compression reduces the encoded size of a JavaScript resource for network transfer. The browser receives the compressed representation and decompresses it before executing the original JavaScript, so compression does not change the program's behavior.
Gzip and Brotli are common general-purpose HTTP content-coding formats. Brotli often achieves a smaller compressed representation for web assets, while Gzip remains widely supported and is a practical fallback. The actual benefit depends on the source content, compression settings, and whether the response is compressed at all.
Compression is different from minification and bundling. Minification removes unnecessary syntax such as whitespace and comments, while bundling combines modules or files. Compression operates on the resulting bytes and can usually be applied after those transformations.
Servers and CDNs typically negotiate compression using HTTP headers. A browser can advertise supported encodings with `Accept-Encoding`, and the server can select an encoding through `Content-Encoding`. Correct caching should account for the selected encoding, commonly through `Vary: Accept-Encoding` when multiple representations are served.
Key points
- Compression reduces network transfer bytes while preserving JavaScript semantics.
- Brotli and Gzip are common HTTP compression formats for JavaScript assets.
- Minification, bundling, and compression are separate optimization stages.
- Compression happens to the bytes being transferred; the browser decompresses them before execution.
- HTTP content negotiation commonly uses `Accept-Encoding` and `Content-Encoding`.
Interview questions
Why should a frontend application compress JavaScript files?
Compression reduces the number of bytes that must travel from the server or CDN to the browser. Smaller responses can reduce network transfer time, especially on slower connections. The browser decompresses the response before using the JavaScript, so the application's semantics are preserved. Compression is therefore mainly a network-transfer optimization.
What are Gzip and Brotli used for?
Gzip and Brotli are compression formats that can be used to encode HTTP responses. A browser advertises supported encodings, and the server or CDN selects an appropriate representation. JavaScript assets are common candidates because their text-based structure generally compresses well. Brotli is widely used for modern web delivery, while Gzip remains an important compatibility option.
How is compression different from minification?
Minification transforms JavaScript into a smaller source representation by removing unnecessary syntax and sometimes shortening identifiers. Compression then encodes the resulting bytes using an algorithm such as Brotli or Gzip. A production frontend commonly uses both because they optimize different stages of delivery. Neither should change the intended runtime behavior when correctly configured.
What HTTP headers are involved in compressed responses?
The browser can send `Accept-Encoding` to indicate which content codings it understands. The server can then return a selected encoding and identify it with `Content-Encoding`. If a cache stores different representations based on this negotiation, `Vary: Accept-Encoding` can indicate that the request header affects the response representation. These headers help clients and intermediaries handle compressed resources correctly.
A JavaScript bundle is still very large after enabling Brotli. What would you investigate?
I would first distinguish transfer size from the uncompressed bundle size and inspect the generated assets. I would analyze dependency composition, duplicate modules, unused code, source maps, and whether tree shaking and code splitting are working as intended. I would also verify that the production response is actually being served with the expected `Content-Encoding`. Compression is useful, but it cannot remove application code that should not have been shipped in the first place.