CompressionStream API: Optimize Your Data Transfers Natively

On the modern web, speed is a priority. Every byte sent between a server and a user impacts loading time. Heavier data makes applications feel slow, especially on mobile devices or poor connections.
Until recently, client-side data compression required importing massive third-party libraries like pako or zlib.js. These tools add weight to your application before it even starts working. Today, a built-in solution exists: the CompressionStream API.
Why compress data on the client side?
Most developers think compression is a task reserved for the server. This is often true for downloading pages. However, in data-rich applications, the client (the browser) often needs to send large amounts of information to the server.
Here are some situations where native compression is useful:
- Sending logs: If your application sends detailed error reports.
- Saving documents: For online text or drawing editors.
- Local storage: To save more information in the browser database (IndexedDB) without saturating the user’s disk space.
What is the CompressionStream API?
This programming interface (API) allows you to compress and decompress data streams natively. “Native” means the code is already present in the browser. You do not need to download anything extra.
It uses streams. A stream is a way to process data bit by bit, like water flowing through a pipe, rather than waiting for the entire bucket to be full. This prevents using too much memory (RAM) on the user’s device.
Available algorithms
The API offers three main formats to transform your data:
- GZIP: The most common format on the Internet. It offers an excellent balance between final file size and calculation speed.
- DEFLATE: A very fast basic algorithm.
- DEFLATE-RAW: A version of Deflate without header information, used for specific technical needs.
Practical application: compressing data
To use this API, we create a compression stream. Here is how to transform simple text into compressed data.
/**
* Compresses a string into GZIP
* @param {string} inputData
* @returns {Promise<Uint8Array>}
*/
async function compressData(inputData)
{
const
encoder = new TextEncoder(),
rawData = encoder.encode(inputData),
// Create a stream from raw data
stream = new Blob([rawData]).stream(),
compressionStream = new CompressionStream('gzip'),
// Pass data through the compressor
compressedStream = stream.pipeThrough(compressionStream),
response = new Response(compressedStream),
buffer = await response.arrayBuffer();
return new Uint8Array(buffer);
}
Doing the opposite: decompression
If you receive compressed data from the server or your local database, you must make it readable again.
/**
* Decompresses GZIP data to get text
* @param {Uint8Array} compressedData
* @returns {Promise<string>}
*/
async function decompressData(compressedData)
{
const
decompressionStream = new DecompressionStream('gzip'),
// Create a readable stream for the decompressor
stream = new ReadableStream(
{
start(controller)
{
controller.enqueue(compressedData);
controller.close();
}
}),
decompressedStream = stream.pipeThrough(decompressionStream),
response = new Response(decompressedStream);
return await response.text();
}
The importance of streaming
The strength of this API lies in its ability to process huge files without freezing the computer. If you try to compress a 1 GB file all at once, the browser might close the tab due to lack of memory.
With the pipeThrough system, data flows in small segments. This is called backpressure management. The system
automatically adapts to the processing speed of the device.
Design optimization and accessibility
Technical performance is not enough. The user must understand what is happening. If compression takes time, a fluid progress bar should be displayed.
Let’s use modern CSS to create an indicator that adapts to all screen sizes using fluid properties.
.progress-container {
/* Use clamp for fluid and responsive width without media queries */
inline-size : clamp(15rem, 50vw + 2rem, 100%);
background-color : #f0f0f0;
border-radius : .5rem;
padding : .25rem;
}
.progress-bar {
block-size : 1rem;
background-color : #007bff;
border-radius : .25rem;
/* Optimization: Only apply transition if the user doesn't prefer reduced motion */
transition : width .2s ease-in-out;
}
/* Use :not to avoid CSS overrides and keep the code clean */
@media (prefers-reduced-motion : reduce) {
.progress-bar:not(.static) {
transition : none;
}
}
Impact on SEO
Google and other search engines use robots to analyze your site. These robots reward sites that are lightweight and resource-efficient.
- Reduction in total weight: Less JavaScript code to download means a better “Core Web Vitals” score.
- Interaction speed: By compressing outgoing data, you free up the user’s bandwidth faster, making the application more responsive.
- Mobile experience: A site that saves its users’ mobile data is better ranked and more appreciated.
When to avoid using this API?
There are cases where compression is useless or even counterproductive:
- Already compressed files: Images (JPG, PNG), videos (MP4), and PDF files are already compressed. Trying to compress them again with GZIP can sometimes make the file larger.
- Small data: If your text is less than 100 bytes, compression will add structure information that will make the final result larger than the original.
Browser compatibility
The CompressionStream API is available on almost all recent browsers (Chrome, Firefox, Safari, Edge). However, for older systems, it is important to check if the tool exists before using it.
console.log(
('CompressionStream' in window)
? 'Native compression is available!'
: 'Using a slower alternative method.'
);
Conclusion: a step towards a cleaner web
Using the CompressionStream API is a smart decision for any performance-conscious developer. It allows for removing unnecessary dependencies, reducing memory consumption, and improving the overall user experience.
In 2026, the quality of an application is not only measured by its features, but by its ability to be lightweight and respectful of the user’s resources. Native compression is one of the best tools to achieve this goal.
