I run a set of image tools that work entirely in the browser, with nothing uploaded. One of them converts PNG to WebP, and I assumed the answer to "is WebP smaller than PNG?" was always yes. Then I measured it, and for lossless WebP made by Chrome's built-in encoder, the answer was no.
Here is what I found, how I fixed it, and the three traps I hit on the way.
The test
I took six kinds of image, each saved as a normal PNG: five photographs, two website screenshots, a text-heavy page, a bar chart, and a logo with a transparent background. I ran them through the real converter page in headless Chromium, and compared four things:
- the original PNG
- the PNG re-saved with optimisation on
- lossless WebP from
canvas.toBlob('image/webp', 1), which is what the browser gives you for free - lossless WebP from libwebp, the reference encoder
For the lossless files I checked every opaque pixel against the original. They matched in both WebP columns, so the only difference is size.
The result
| Image | PNG | PNG optimised | WebP lossless (browser) | WebP lossless (libwebp) |
|---|---|---|---|---|
| Photos (5, combined) | 2,997 KB | 2,925 KB | 2,626 KB | 2,449 KB |
| Home-page screenshot | 292 KB | 273 KB | 310 KB | 197 KB |
| Tool-page screenshot | 124 KB | 126 KB | 151 KB | 76 KB |
| Text-heavy page | 234 KB | 241 KB | 280 KB | 76 KB |
| Bar chart | 21 KB | 21 KB | 90 KB | 5.2 KB |
| Transparent logo | 13 KB | 10 KB | 16 KB | 5.6 KB |
The browser's lossless WebP was larger than the PNG for five of the six types (only the photographs came out smaller). The chart grew from 21 KB to 90 KB. libwebp, on the same pixels, made it 5.2 KB, which is 76% smaller than the PNG. On the text page it was 67% smaller, and on photographs 18% smaller.
I measured the sizes, not the reason. My guess is that a browser encoder is tuned for speed, since it is called from a canvas and has to return quickly, and a reference encoder spends more effort looking for savings. Treat that as a hypothesis.
The fix: libwebp compiled to WebAssembly
I used @jsquash/webp, which ships libwebp as WebAssembly (Apache-2.0). The two .wasm files are about 280 KB and 350 KB (the second uses SIMD), and they load only when someone asks for a lossless WebP, so the rest of the site does not pay for them.
The encoder runs in a module worker so a big image does not freeze the page. It picks the SIMD build by feature detection:
const simd = WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,10,1,8,0,65,0,253,15,253,98,11]));
const m = await import(simd ? './webp_enc_simd.js' : './webp_enc.js');
const mod = await m.default({ noInitialRun: true });
const out = mod.encode(new Uint8Array(buf), width, height, options);
The page sends the pixels from getImageData, transferring the buffer so nothing is copied:
const img = canvas.getContext('2d').getImageData(0, 0, canvas.width, canvas.height);
worker.postMessage({ id, buf: img.data.buffer, width: canvas.width, height: canvas.height, options }, [img.data.buffer]);
If the worker cannot load, for example on an old browser, the code falls back to the browser's own encoder. A converter that sometimes returns a bigger file is better than one that returns nothing.
Trap 1: the effort setting
libwebp has an effort setting called method, from 0 to 6. I benchmarked it on the real pages. Method 6 saved a further 0 to 14% over method 4, but took 6 to 10 times longer on screenshots and photos. Method 4 stays under about a second on these images, so that is the default, and for images over 8 megapixels I drop to method 2. For a tool people use interactively, a second feels instant and ten seconds feels broken.
Trap 2: canvas alpha is not exact
A canvas stores colours premultiplied by alpha, so a half-transparent pixel can come back a level or two different after a round trip, and a fully transparent pixel loses its colour completely. In my test, opaque pixels were identical, and half-transparent pixels differed by at most 1 level out of 255. If you need "bit-for-bit lossless" for transparent images, a canvas-based tool cannot promise it, and I now say so on the page.
Trap 3: workers and .wasm over file://
A module worker will not load from file:// in Chrome, so my own tests failed until I served the pages over HTTP. On the server side, .wasm files need the application/wasm MIME type, or the browser refuses to compile them streaming. On Apache that is one line in .htaccess:
AddType application/wasm .wasm
What I would do again
- Measure before you believe a rule of thumb. "WebP is smaller" is reliably true for lossy WebP, and only sometimes true for lossless.
- Ship the heavy dependency lazily, behind the one feature that needs it.
- Keep a fallback.
Limits
Six image types is a sample, not a survey, and your images will give different percentages. I tested one browser encoder (Chromium's) and one full-effort encoder, and I did not test AVIF or animated WebP.
The full tables, the lossy results at 85% quality, and the transparency checks are in the longer write-up: WebP vs PNG, measured. You can try the converter at convertpicto.com/png-to-webp, and the code is on GitHub.
Top comments (0)