DEV Community

Cover image for browser-image-compression vs compressorjs vs Squoosh: I Squeezed the Same Photo to 20 KB
Hannan Shoukat for 8Bit Enterprise

Posted on AI-assisted

browser-image-compression vs compressorjs vs Squoosh: I Squeezed the Same Photo to 20 KB

Lots of upload forms have a hard file size limit: "photo under 20 KB", "signature 10 to 20 KB". If you compress in the browser before uploading, the file has to come out under that number, not near it.

I put four common approaches through the same test and measured what each one actually produced.

Disclosure up front: I build an image compressor that runs in the browser, and it uses one of these four approaches (the Squoosh codec). So I've also run my own tool on the same photo and included its numbers, including the place where it makes a trade-off you might not like.

The setup

  • Photo: a 6000×4000 JPEG (3,879,929 bytes) by Andrew Neel on Unsplash. Wood grain, a brick wall and handwriting on a notepad, the kind of detail JPEG struggles with.
  • Goal: long side 1200 px, file size at most 20,000 bytes (and a second run at 50,000). Bytes, not KiB, because that's how forms count "20 KB".
  • Browser: Chrome 145 on Linux. Times are the median of 3 runs and include the whole search for the right quality.
  • Quality score: SSIM on luma in 8×8 windows, against the same photo resized to the same size with no compression. 1.0 means identical.

The four contenders:

  1. browser-image-compression 2.0.2, with maxSizeMB set to exactly 20,000 bytes (20000 / 1048576, since the library counts 1 MB as 1,048,576 bytes) and maxWidthOrHeight: 1200.
  2. compressorjs 1.4.0. It has no "target size" option, so I binary-searched its quality (10 steps).
  3. Plain canvas.toBlob (well, OffscreenCanvas.convertToBlob), binary search on quality.
  4. Squoosh's MozJPEG encoder via jSquash, the WASM codec from Google's Squoosh app packaged as an npm module, binary search on integer quality 0 to 100.

Results at 20,000 bytes

Method Bytes Under 20 KB? Size SSIM Time
browser-image-compression 22,325 No 716×475 0.822 564 ms
compressorjs 19,006 Yes 1200×800 0.900 3,560 ms
canvas.toBlob 19,900 Yes 1200×800 0.900 557 ms
Squoosh MozJPEG (jSquash) 19,927 Yes 1200×800 0.919 601 ms

2x crops of each 20 KB result

Three things stood out:

browser-image-compression returned a file over the limit, and smaller. It gave back 22,325 bytes at 716×475, even though I asked for 1200 px and 20,000 bytes. A form with a 20 KB limit would reject that file.

MozJPEG won on quality at the same size. Same 1200×800, same ~19.9 KB, visibly sharper handwriting in the crop, and the highest SSIM.

compressorjs and canvas were basically the same encoder, which makes sense: both end up in the browser's own JPEG encoder. compressorjs was 6× slower in my setup because each call takes the original File, so every step of the search decoded the full 24 MP photo again. Canvas and jSquash decoded once and re-encoded the small version.

At 50,000 bytes, the gap closes

Method Bytes Size SSIM
browser-image-compression 42,059 880×586 0.885
compressorjs 49,827 1200×800 0.957
canvas.toBlob 49,861 1200×800 0.965
Squoosh MozJPEG (jSquash) 49,529 1200×800 0.965

With more bytes to spend, canvas and MozJPEG tied. MozJPEG's advantage shows up when the budget is tight, which is exactly the 10 to 20 KB form case. browser-image-compression stayed under this time, but again gave away resolution: 880 px instead of 1200.

Why browser-image-compression overshoots

I read the library's dist code to see what was going on. Each iteration it multiplies the JPEG quality by 0.95 (starting from initialQuality, default 1) and, unless alwaysKeepResolution is set, also scales the image down by 5%. It stops after maxIteration rounds, default 10.

Ten rounds of ×0.95 gives quality 0.60 and a scale of 0.60, which is why 1200 px became ~716 px. Canvas needed quality ~0.135 to fit 20 KB at 1200 px, and getting from 1.0 to 0.135 in 0.95 steps takes about 39 rounds.

So I tried its options:

Options Bytes Under? Size
default 22,325 No 716×475
maxIteration: 30 19,452 Yes 680×451
alwaysKeepResolution: true 70,761 No 1200×800
alwaysKeepResolution + maxIteration: 40 20,747 No 1200×800
initialQuality: 0.2 + alwaysKeepResolution 19,532 Yes 1200×800

initialQuality: 0.2 does it. The catch is that the right starting quality depends on the photo, so you'd be guessing per image. It's not a bug, just a fixed step-down rather than a search.

Where my own tool lands

My tool uses the MozJPEG-plus-search approach from above (in a Web Worker), with one deliberate difference: a quality floor. It won't go below MozJPEG quality 40, because below that photos turn blocky. If the limit can't be reached at the requested size without going under the floor, it shrinks the image instead.

That's exactly what happened here: the test above needed quality 22 to fit 20 KB at 1200 px, which is below the floor.

Same photo, same settings (at most 1200 px wide, under the limit), run on the live tool:

Target Bytes Under? Size SSIM (at its own size)
20,000 19,779 Yes 862×575 0.947
50,000 49,820 Yes 1200×800 0.964

So at 20 KB it also gave up resolution, like browser-image-compression did, but it stayed under the limit and kept more pixels (862 px vs 716 px). At 50 KB it matched the best results above. It's a trade-off, not a free win: a smaller, cleaner photo instead of a full-size blocky one. If you need exact dimensions, its exact-size mode keeps the pixels and tells you when the limit can't be met at that size, rather than handing back a file that's over it.

One caveat on that SSIM: it's measured at 862×575 against the original resized to 862×575, so it isn't directly comparable to the 1200×800 scores in the first table.

The pattern I ended up using

If you need "as good as possible, but under N bytes", a binary search on quality at a fixed size works with any encoder. Here's the jSquash version:

import { encode } from '@jsquash/jpeg';

// Highest quality that fits under maxBytes, or null if nothing fits
async function fitUnder(imageData, maxBytes) {
  let lo = 0, hi = 100, best = null;
  while (hi - lo > 1) {
    const q = Math.round((lo + hi) / 2);
    const buf = await encode(imageData, { quality: q });
    if (buf.byteLength <= maxBytes) { best = buf; lo = q; } else { hi = q; }
  }
  if (best) return best;
  const floor = await encode(imageData, { quality: 0 });
  return floor.byteLength <= maxBytes ? floor : null;
}
Enter fullscreen mode Exit fullscreen mode

Resize once to your target dimensions, get the ImageData, then search. It took 7 encodes here. If it returns null, shrink the dimensions and search again, rather than silently handing back a file over the limit.

Bundle cost, measured with gzip:

Raw Gzipped
browser-image-compression 57 KB 20.5 KB
compressorjs 11.4 KB 4.7 KB
jSquash MozJPEG encoder (JS + WASM) 290 KB 70 KB
canvas 0 0

MozJPEG costs about 70 KB gzipped, so lazy-load it when someone actually picks a file.

What this test doesn't show

One photo, one browser, one machine. Different photos compress differently, Safari's and Firefox's JPEG encoders aren't Chrome's, and my SSIM is a simple 8×8-window version, so treat the scores as relative, not absolute. I also ran without web workers to keep timings comparable. If you get different numbers on your photos, I'd genuinely like to see them.

So, what do you use?

When you compress images before upload, what are you using, and have you ever had a file come out over the limit? And for the tight 10 to 20 KB cases, is a 70 KB WASM codec worth it to you, or is canvas good enough?

Photo: Andrew Neel on Unsplash, Unsplash License.

Top comments (0)