Back to writing

notes · 2026-09-25

Never hand back a bigger file

A small rule in TinyMedia's compressor, and the two cases where it has to step aside.

2 minweb-performanceimage-compressionproduct-decisions

TinyMedia compresses images in the browser. Re-encoding does not always make a file smaller: an already optimized JPEG, or a PNG sent through the encoder again, can come out bigger than it went in. A compressor that returns that file has made things worse, and it still presents the file as a result.

So the rule is simple. After encoding, compare the sizes. If the output is the same size or larger, return the original untouched and mark it as unchanged. The interface then shows “Original kept” instead of a negative saving. The audio engine applies the same idea earlier: when the settings ask for no change at all (same format, no trim, no normalization), it skips FFmpeg entirely.

The interesting part is the exceptions. HEIC photos are exempt: people drop them precisely to get a JPEG or WebP they can use, and HEIC often compresses better than the re-encode, so keeping the original would quietly defeat the feature. Colour LUTs are exempt too: the user asked for the colours to change, and returning the smaller original would give them a file different from the one they previewed.

In both cases, the output differs from the source on purpose, not just in size. The size check only applies when size is the whole point.

Keep reading

Never hand back a bigger file — Tiago Pinto