Image Optimization for Web Performance Fix
In this tutorial, you'll learn about Image Optimization for Web Performance Fix. We cover key concepts, practical examples, and best practices.
Your website loads slowly and Lighthouse reports "Properly size images" and "Serve images in next-gen formats" β large PNG/JPEG images are served at resolutions much higher than the display size, or old formats are used instead of WebP/AVIF.
Step-by-Step Fix
1. Find oversized images
Use Chrome DevTools:
- Open DevTools > Network > Img filter
- Sort by Size
- Click an image to see the natural dimensions vs display dimensions
2. Use responsive images with srcset
<!-- Wrong β one image for all screen sizes -->
<img src="hero-1920.jpg" alt="Hero image">
<!-- Right β serve different sizes for different screens -->
<img src="hero-480.jpg"
srcset="hero-480.jpg 480w,
hero-768.jpg 768w,
hero-1024.jpg 1024w,
hero-1920.jpg 1920w"
sizes="(max-width: 480px) 480px,
(max-width: 768px) 768px,
(max-width: 1024px) 1024px,
1920px"
alt="Hero image">
3. Convert to WebP format
# Convert JPEG to WebP
cwebp -q 80 input.jpg -o output.webp
# Convert PNG to WebP
cwebp -q 90 input.png -o output.webp
# Expected output
# Saved file output.webp (originally 500KB -> now 120KB, 76% smaller)
<!-- Wrong β only using JPEG -->
<img src="photo.jpg" alt="Photo">
<!-- Right β use <picture> with WebP fallback -->
<picture>
<source srcset="photo.webp" type="image/webp">
<source srcset="photo.jpg" type="image/jpeg">
<img src="photo.jpg" alt="Photo">
</picture>
4. Compress with lossy optimization
# JPEG optimization (lossy)
jpegoptim --max=85 --strip-all input.jpg
# PNG optimization (lossless)
optipng -o7 input.png
Common Mistakes
| Mistake | Fix |
|---|---|
| Using 4000px wide images when display is 800px | Use responsive srcset with appropriate sizes |
| Serving PNG for photographs | Use JPEG or WebP for photos, PNG only for transparency |
| No lazy loading below the fold | Add loading="lazy" to all images not in the initial viewport |
| No compression on uploaded images | Compress server-side during upload |
| Ignoring WebP browser support | Use <picture> with JPEG/PNG fallbacks |
Prevention
- Set maximum image dimensions in the CMS or upload pipeline.
- Automatically convert all uploaded images to WebP.
- Use an image CDN (Cloudinary, imgix) for automatic optimization.
- Set up a build step that compresses and converts all static images.
- Use Lighthouse CI to enforce image budget in CI/CD.
DodaTech Tools
Doda Browser automatically optimizes images for bandwidth-constrained connections using WebP conversion. DodaZIP compresses image archives with metadata stripping for privacy. Durga Antivirus Pro includes image scanning capabilities that process compressed formats efficiently.
Common Mistakes with optimization
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
These mistakes appear frequently in real-world IMAGE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro