Images are usually the heaviest part of a web page, and the part developers optimise last. On a typical content site, images account for more than half of all bytes transferred. Optimising them is often the single biggest performance win available — and it is also one of the cheapest to implement.
The cost of an unoptimised image
Consider a common scenario: a hero image uploaded straight from a camera at 6000 × 4000 pixels and 8 MB, displayed in a slot that is 1200 pixels wide. The browser downloads 8 MB to show a fraction of it. On a 4G connection that is several seconds of loading, and on a metered mobile plan it is real money. Multiply that by a page with ten images and the page is slow before any JavaScript has run.
The cost is not only loading time. Large images block rendering, push the Largest Contentful Paint (LCP) later, and compete with critical resources for bandwidth. An unoptimised image can be the difference between passing and failing Core Web Vitals.
Choose the right format
Format choice matters more than most developers think:
- AVIF gives the best compression for photographs, often 30–50% smaller than JPEG at the same quality. Support is now good in all modern browsers.
- WebP is the safe middle ground: widely supported, much smaller than JPEG and PNG.
- JPEG is the fallback for older browsers.
- PNG is only for images that need transparency or sharp edges, like logos and diagrams — never for photographs.
- SVG is for icons and simple graphics, where it is infinitely scalable and tiny.
Serve AVIF or WebP with a JPEG fallback using the <picture> element, or let your image CDN handle format negotiation automatically.
<picture>
<source srcset="photo.avif" type="image/avif">
<source srcset="photo.webp" type="image/webp">
<img src="photo.jpg" width="1200" height="800" alt="">
</picture>
Resize, don't just compress
Compression helps, but resizing helps more. An image should never be larger than the largest size at which it is displayed. Use responsive images so each device gets the size it needs:
<img src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 800px"
width="800" height="533" alt="">
A phone on a small screen gets the 400w file; a desktop gets the 800w or 1600w file. Nobody downloads more than they need.
Lazy loading and decoding
Images below the fold should be lazy-loaded with loading="lazy", so they are only fetched when the user scrolls near them. Images above the fold — especially the hero — should load eagerly, with fetchpriority="high". Getting this backwards is a classic mistake: lazy-loading the hero image delays LCP by the time it takes the browser to notice it is needed.
Add decoding="async" to images so decoding does not block the main thread, and always set width and height so the browser can reserve space and avoid layout shift (CLS).
Automate it
Manual optimisation does not scale. The reliable approach is to automate it in the build or at the edge:
- Build-time: tools like Sharp, Squoosh or ImageMagick can generate resized, multi-format variants of every image as part of the build.
- CDN: an image CDN (Cloudinary, Imgix, or Cloudflare Images) can resize, convert and compress on the fly from URL parameters.
- CMS: most modern CMSs generate responsive variants automatically — check that yours is configured to do so, and that the templates actually use the variants.
Whatever the pipeline, add a check: a CI step that fails the build if an image over a size threshold is committed, or a Lighthouse audit that flags oversized images, will catch regressions before they reach production.
Measuring the difference
Take one heavy page, optimise its images, and compare the before and after in PageSpeed Insights. It is common to see LCP drop by a second or more, total page weight fall by 70%, and the mobile score jump an entire colour band. That is the result of changing a handful of files — no architecture change, no framework migration, no rewrite.
Image optimisation is not glamorous work. But it is the highest-return performance work most sites have available, and it is the work users feel most directly: a page that appears instantly instead of one that stutters in, image by image.
Top comments (0)