Image Optimization for Web Performance
Images typically account for 50–70% of a webpage's total size — making them the single biggest lever for improving load times. Get this wrong, and no amount of JavaScript optimization will save your Core Web Vitals scores.
Why Image Optimization Matters for Deployment
Every deployment ships assets to production. If your build pipeline doesn't handle image optimization, you're pushing unoptimized files to your servers on every release. The best approach is automating image processing as part of your deployment workflow so optimized images ship by default — not as an afterthought.
Modern Formats
Use WebP and AVIF for 30-50% smaller file sizes.
Responsive Images
Serve different sizes for different devices and screens.
Lazy Loading
Load images only when they enter the viewport.
Compression
Reduce file size without visible quality loss.
Modern Image Formats
WebP
- •25–35% smaller than JPEG at equivalent quality
- •Supports transparency and animation
- •Broad browser support — safe for production use
- •Use JPEG fallback for the remaining edge cases
AVIF
- •50% smaller than JPEG — the most efficient format available
- •Better quality at same file size compared to WebP
- •Supported in all modern browsers and growing rapidly
- •Use with WebP fallback for maximum compatibility
Format Comparison
| Format | Best For | Size vs JPEG | Transparency |
|---|---|---|---|
| JPEG | Photographs, complex images | Baseline | No |
| PNG | Logos, icons, sharp edges | 2–5x larger | Yes |
| WebP | General-purpose replacement for JPEG & PNG | 25–35% smaller | Yes |
| AVIF | Maximum compression, photos & illustrations | ~50% smaller | Yes |
| SVG | Vector graphics, icons, illustrations | Resolution-independent | Yes |
| GIF | Simple animations (prefer video for complex ones) | Much larger | Limited |
Rule of thumb: Use AVIF with WebP fallback for photos, SVG for icons and logos, and avoid GIF — use <video> for animations instead.
Implementation
Use the <picture> element to serve the best format each browser supports:
<picture> <source srcset="image.avif" type="image/avif"> <source srcset="image.webp" type="image/webp"> <img src="image.jpg" alt="Description"></picture>Pro tip: Most build tools (Vite, Webpack, Next.js) can automate format conversion at build time. If you're using build pipelines in your deployment, add an image processing step so format conversion happens automatically on every deploy.
Responsive Images
Serving a 2000px image to a mobile screen wastes bandwidth and kills load times. Use srcset to let the browser pick the right size:
srcset and sizes
<img srcset="small.jpg 500w, medium.jpg 1000w, large.jpg 2000w" sizes="(max-width: 600px) 500px, (max-width: 1200px) 1000px, 2000px" src="medium.jpg" alt="Description">This alone can reduce image transfer size by 40–60% on mobile devices.
Lazy Loading
Native Lazy Loading
The simplest approach — one attribute, no JavaScript required:
<img src="image.jpg" loading="lazy" alt="Description">Above the Fold Priority
- •Never lazy load hero images or LCP (Largest Contentful Paint) elements
- •Add
fetchpriority="high"to your most important above-the-fold image - •Lazy load everything below the fold
- •This distinction directly impacts your LCP score in Core Web Vitals
<!-- Hero image: load immediately with high priority --><img src="hero.jpg" fetchpriority="high" alt="Hero image">
<!-- Below fold: lazy load --><img src="feature.jpg" loading="lazy" alt="Feature image">Compression
Lossy Compression
- •JPEG: Quality 80–85 is the sweet spot — below 80 artifacts become visible, above 85 savings plateau
- •WebP: Quality 75–80 delivers excellent results
- •Tools: TinyPNG, ImageOptim, Squoosh, Sharp (Node.js)
Lossless Compression
- •PNG: Use only for graphics requiring transparency or sharp edges (logos, icons)
- •Optimize with pngquant or OptiPNG — typically saves 30–50% without quality loss
- •Consider SVG for vector graphics instead of PNG
Automating Compression in Your Build
Rather than manually compressing images, integrate it into your build pipeline with Sharp:
const sharp = require('sharp');const glob = require('fast-glob');
async function optimizeImages() { const files = await glob('src/images/*.{jpg,png}'); for (const file of files) { await sharp(file) .resize(1600, null, { withoutEnlargement: true }) .webp({ quality: 80 }) .toFile(file.replace('src/', 'dist/').replace(/\.(jpg|png)$/, '.webp')); }}
optimizeImages();If you're deploying with DeployHQ, you can add image optimization as a build command that runs before every deployment — ensuring no unoptimized image ever reaches production.
Image CDN
For sites with many images or user-generated content, an image CDN handles optimization automatically:
- •Automatic format conversion — serves WebP/AVIF based on browser support
- •On-the-fly resizing — generates thumbnails and responsive sizes without pre-processing
- •Global edge caching — serves images from the nearest PoP to the user
- •Smart compression — adjusts quality based on image content and network conditions
Popular options: Cloudinary, Imgix, Cloudflare Images, and framework-specific solutions like Next.js Image Optimization.
Dimensions & Layout Shift Prevention
Always set explicit dimensions to prevent Cumulative Layout Shift (CLS):
<img src="image.jpg" width="800" height="600" alt="Description">Or use CSS aspect-ratio for fluid layouts:
img { aspect-ratio: 16 / 9; width: 100%; height: auto;}Why this matters: CLS is a Core Web Vitals metric. Images without dimensions cause page content to jump as they load — frustrating users and hurting your performance score.
Optimization Checklist
- Use modern formats (WebP with AVIF where supported)
- Implement responsive images with
srcset - Lazy load below-the-fold images
- Set
fetchpriority="high"on LCP images - Compress to quality 80–85 (JPEG) or 75–80 (WebP)
- Always set width/height or aspect-ratio
- Automate optimization in your build pipeline
- Use an image CDN for dynamic or user-generated content
See How Your Images Score
Get AI-powered recommendations for image optimization tailored to your site.
More guides: Core Web Vitals explained · JavaScript Optimization · Mobile Performance