Frontend System Design · Lesson 46 of 86 · 4 min
Modern Image Formats: WebP, AVIF, JPEG XL
This is a classic performance optimization question. Knowing the trade-offs shows you can build fast websites, not just functional ones.
The core trade-off: file size vs. quality
For years, we've balanced JPEG's lossy compression for photos against PNG's lossless quality for graphics. This is a false dichotomy now. Modern formats like WebP and AVIF offer much better compression, meaning you can get higher quality images for significantly smaller file sizes.
WebP typically creates files 25-35% smaller than an equivalent quality JPEG. AVIF can be 30-50% smaller than WebP. These savings directly reduce page load time and data usage, which are critical for Largest Contentful Paint (LCP) and user experience on mobile.
These formats aren't just smaller. They also support features that required separate formats before, like alpha transparency (like PNG) and animation (like GIF), all in one package.
Your modern image toolbox: WebP, AVIF, JPEG XL
In an interview, you need to know the specific use case for each. Don't just say "they're smaller."
WebP: This is your baseline modern format. It has near-universal support across modern browsers and has been around long enough to be stable and well-supported by tooling. It offers both lossy and lossless compression. If you can only support one modern format, this is it.
AVIF: This is your high-performance option. Based on the AV1 video codec, it provides the best compression available in browsers today. It also supports modern features like HDR and wide color gamuts. Use it for hero images, product photos, or any visually critical content where you need the absolute smallest file size for a given quality level.
The trap: Browser support for AVIF is good, but not as universal as WebP. You can't serve only AVIF. It must be part of a fallback strategy.
JPEG XL: This is the one to watch. Its killer feature is the ability to losslessly re-compress existing JPEGs, reducing their size by ~20% without any quality degradation. This is a massive advantage for migrating large, existing image libraries. It also has excellent compression and progressive rendering.
The interview landmine: As of late 2023, Chrome removed support for JPEG XL, and no major browser supports it by default. Mentioning it shows you're knowledgeable about the future of image formats, but presenting it as a solution you'd implement today is a red flag. Frame it as something to keep an eye on for future-proofing a system.
Your decision flow should be: serve AVIF if possible, fall back to WebP, and finally fall back to the original JPEG or PNG.
How to serve modern formats without breaking old browsers
You don't use JavaScript user-agent sniffing. You use the <picture> element to let the browser choose for itself.
This is the standard, declarative way to provide multiple formats. The browser works down the list of <source> elements and picks the first one it supports based on the type attribute. If it supports none, it uses the <img> tag as a final fallback.
<picture>
<!-- The browser will check for AVIF support first. -->
<source srcset="image.avif" type="image/avif" />
<!-- If it doesn't support AVIF, it checks for WebP. -->
<source srcset="image.webp" type="image/webp" />
<!-- The img tag is the final fallback for all other browsers. -->
<img src="image.jpg" alt="A description of the image." width="800" height="600" />
</picture>
In an interview, you should be able to write this
<picture>block from memory. It's fundamental. Also, be sure to mention that you'd includewidthandheightattributes on the<img>tag to prevent Cumulative Layout Shift (CLS). This shows you're thinking about the full picture of web performance, not just asset size.
This pattern is combined with the srcset and sizes attributes (covered in the previous lesson) to handle responsive images and multiple formats simultaneously.
The backend: Where do these images come from?
You don't manually create three formats and multiple sizes for every image. This process must be automated. In a system design interview, this is the part you'll spend more time on.
The images are generated in a pipeline, which can be triggered either at build time or on-the-fly.
There are two common architectures for this:
-
Build-Time Generation: Frameworks like Next.js (with
next/image) or static site generators can process local images during the build. They generate the required formats and sizes, which are then deployed with your static assets. This is great for sites where the images don't change often, like a marketing page or blog. -
On-the-Fly Transformation (Image CDNs): For sites with user-generated content or massive image libraries (e-commerce, social media), you upload one high-resolution original. An image CDN service (like Cloudinary, Imgix, or self-hosted solutions) then creates different versions dynamically on request. The URL itself specifies the transformations, like
.../image.jpg?format=avif&width=800. The CDN heavily caches these transformed images so they are only generated once.
Knowing the difference between these two approaches is key. The build-time approach is simpler and has no runtime cost, but is inflexible. The dynamic approach is powerful and scalable, but adds a service dependency and cost.
Try it
Take a high-quality JPEG from one of your projects (at least 1MB). Use an online tool like Squoosh to convert it to WebP and AVIF. Adjust the quality slider for each to find the point where they are visually indistinguishable from the original JPEG. Note the file size difference. Then, create a local HTML file with the <picture> element to serve all three versions and test it in your browser's network tab.
Next: We'll cover how to stop loading images until they're actually needed in Lazy Loading Images and Videos.
Lock it in — apply this lesson under interview conditions.