webdevpuneet.com

CompressionStream API Demo — Free Real Gzip-in-Browser Tool

CompressionStream API Demo · Visualizers · Plain HTML, CSS & JS · Live preview

CategoryVisualizers

Copy & paste

Source Code

<section class="csd-wrap">
  <span class="csd-tag">compressionstream api · gzip in the browser</span>
  <h1>Compress text, client-side</h1>
  <p id="csdStatus">Type or paste text below — it's compressed with the real CompressionStream API, no server involved.</p>

  <textarea class="csd-input" id="csdInput" rows="6" placeholder="Paste some text here… (longer, more repetitive text compresses better)">The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.</textarea>

  <div class="csd-grid">
    <div class="csd-stat"><span id="csdOriginal">0 B</span><small>original size</small></div>
    <div class="csd-stat"><span id="csdCompressed">0 B</span><small>compressed (gzip)</small></div>
    <div class="csd-stat"><span id="csdRatio">0%</span><small>size reduction</small></div>
  </div>

  <div class="csd-verify" id="csdVerify"></div>
  <p class="csd-note" id="csdNote"></p>
</section>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Real gzip via CompressionStream
The browser's native codec, not a JS library.
Streams-based pipeline
Blob.stream().pipeThrough() end to end.
Measured byte counts
TextEncoder + stream draining, never estimated.
Computed ratio
(1 − compressed/original) × 100 from real lengths.
Round-trip verification
Decompresses and string-compares against the input.
Debounced live updates
Recompresses as you type without lag.
Honest small-input behavior
Shows overhead making short text grow, not shrink.
Unsupported-browser fallback
States plainly that no compression can run.

About this UI Snippet

CompressionStream API Demo — Real Gzip Bytes, Not a Guessed Ratio

Screenshot of the CompressionStream API Demo snippet rendered live

This snippet compresses whatever text you type using the browser's built-in CompressionStream, the same gzip/deflate codec the browser already uses under the hood for network responses — now exposed as a streams-based API you can run entirely client-side, with no server round-trip and no bundled compression library.

The real compression pipeline

Text is encoded to bytes with TextEncoder, wrapped in a Blob, turned into a ReadableStream via .stream(), and piped through new CompressionStream('gzip') with .pipeThrough(). The output is another stream, which a small streamToBytes() helper drains chunk by chunk into a single Uint8Array — that array's .length is the actual compressed byte count. Nothing here is estimated: both the "before" and "after" numbers are real TextEncoder/stream output lengths.

A genuine ratio, computed, not chosen

The displayed reduction percentage is literally (1 - compressedBytes.length / originalBytes.length) * 100. Because it's derived from measured byte lengths on both sides, it behaves the way real gzip actually does — highly repetitive text (like the default sample sentence repeated three times) compresses dramatically, while short or already information-dense text can compress *poorly*, sometimes even growing slightly larger once gzip's header and footer overhead is counted. The snippet states this honestly rather than hiding the case where compression "fails" to help.

Proving it round-trips

Compressing bytes proves nothing about correctness on its own, so this demo immediately decompresses its own output with new DecompressionStream('gzip'), decodes the result back to text with TextDecoder, and does a strict string-equality check against the original input. That comparison — not just a "success" message — is the actual proof that the compression is lossless and the byte counts above are trustworthy.

Handling the unsupported case honestly

CompressionStream/DecompressionStream are missing in some older browsers. There's no honest way to fabricate a real gzip byte count without actually running gzip, so rather than fake a plausible-looking ratio, the fallback here plainly states compression cannot run, shows only the real original size, and suggests a JS polyfill library as the real-world workaround. Pair this with a storage quota meter for a broader "what this browser's storage/data APIs can do" dashboard.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSSample text compresses immediately on load.
  2. 2
    Edit the textCompression re-runs (debounced) as you type.
  3. 3
    Read the three statsOriginal size, compressed size, and reduction %.
  4. 4
    Check the verify lineConfirms a real decompress round-trip matches the input.
  5. 5
    Try very short textWatch the ratio go poor or even negative — gzip overhead.
  6. 6
    Try unsupported browsersA clear "cannot run here" message appears instead of fake numbers.

Real-world uses

Common Use Cases

Client-side upload preparation
Shrink text payloads before sending, using the browser's native gzip codec through CompressionStream rather than a JavaScript library.
IndexedDB storage savings
Pair with a storage quota meter to measure how much space compressed cached data actually saves.
Clipboard and export tools
Compress large text exports in the browser, with ratios computed from real byte lengths via TextEncoder and stream draining.
Educational gzip demos
Show how gzip behaves on repetitive versus random text, with a round-trip verification step proving nothing was lost.
Offline-first and log viewers
Compress cached data before writing it, or estimate transfer savings for large logs using the streams-based pipeline.
Related: Dependency Graph Viewer
See the Dependency Graph Viewer for a related dashboards pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

It's real. new CompressionStream('gzip') is the same codec implementation the browser already uses to decode gzip-encoded network responses, now exposed for you to run in the other direction on arbitrary bytes. Both the original and compressed byte counts come from actually encoding and streaming the data, not from an estimate or lookup table.

Gzip adds a fixed amount of header and footer overhead (roughly 18-20 bytes) to every compressed payload. For very short or already low-redundancy text, that overhead can outweigh whatever gzip manages to save, so the "compressed" size ends up larger than the original — a real, well-known gzip behavior this demo shows honestly rather than hiding.

After compressing, the demo immediately runs the compressed bytes back through DecompressionStream, decodes the result to text, and does a strict equality check against your original input. If compression were faked or lossy, that check would fail — so a passing "round-trip verified" message is direct proof the compressed byte count is both real and correct.

The API is feature-detected up front. If it's missing, the demo does not invent a plausible-looking compressed size or ratio — since there is no honest number to show without actually running gzip — and instead states clearly that compression cannot run in this browser, showing only the real, measurable original byte size.

Move the compression/decompression logic into an async function triggered from your input's onChange (debounced), store the resulting byte counts and verification result in component state, and render them from there. The CompressionStream/DecompressionStream/TextEncoder APIs themselves are framework-agnostic and need no adaptation.