JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsperformance
PrevNext

Learn the concept

Image Optimization

performance
junior
images

How do you optimize images for web performance?

images
optimization
lazy-loading
webp
srcset
Quick Answer

Optimize images by: using modern formats (WebP, AVIF), proper sizing for display dimensions, lazy loading below-the-fold images, using responsive images with srcset, and compressing without visible quality loss.

Detailed Explanation

Image Optimization Techniques:

  1. Modern Formats:

    • WebP: 25-35% smaller than JPEG
    • AVIF: Even smaller, newer support
    • Use <picture> for fallbacks
  2. Proper Sizing:

    • Don't serve 2000px for 200px display
    • Generate multiple sizes
    • Use srcset for responsive
  3. Lazy Loading:

    • loading="lazy" attribute
    • Only load when near viewport
    • Eager load above-the-fold
  4. Compression:

    • Balance quality vs size
    • Use tools like Squoosh, ImageOptim
    • Automate in build process

Code Examples

Responsive images with srcsetHTML
<!-- Basic srcset for different sizes -->
<img 
  src="image-800.jpg"
  srcset="
    image-400.jpg 400w,
    image-800.jpg 800w,
    image-1200.jpg 1200w
  "
  sizes="(max-width: 600px) 400px, (max-width: 1000px) 800px, 1200px"
  alt="Description"
  loading="lazy"
  decoding="async"
  width="800"
  height="600"
/>

<!-- Picture element for format fallbacks -->
<picture>
  <source type="image/avif" srcset="image.avif" />
  <source type="image/webp" srcset="image.webp" />
  <img src="image.jpg" alt="Description" loading="lazy" />
</picture>

<!-- Art direction with different images -->
<picture>
  <source media="(max-width: 600px)" srcset="mobile-hero.jpg" />
  <source media="(max-width: 1000px)" srcset="tablet-hero.jpg" />
  <img src="desktop-hero.jpg" alt="Hero" />
</picture>

Real-World Applications

Use Cases

Optimizing product images on an e-commerce website

To ensure fast loading times for product galleries and detail pages, improving user experience and conversion rates. This includes using modern formats like WebP/AVIF, responsive `srcset`, and lazy loading.

Improving hero image and banner performance on a landing page

By preloading critical hero images, specifying dimensions to prevent CLS, and using `<picture>` for art direction and format fallback, ensuring a visually stable and fast-loading above-the-fold experience.

Managing user-uploaded images in a social media application

By automatically resizing, compressing, and converting uploaded images to optimal web formats on the server-side, reducing storage costs and improving delivery speed to users.

Mini Projects

Responsive Image Component

intermediate

Build a custom React or vanilla JavaScript component that handles responsive image loading, including `srcset`, `sizes`, and lazy loading with an Intersection Observer fallback.

Image Optimization CLI Tool

advanced

Create a simple command-line interface tool that uses a library like Sharp or ImageMagick to bulk process and optimize images (resize, convert format, compress) within a project directory.

Industry Examples

Unsplash

Unsplash, a popular stock photography website, uses extensive image optimization techniques, including responsive images, modern formats, and CDNs, to deliver high-quality images quickly to users worldwide.

Smashing Magazine

Smashing Magazine, known for its web development articles, employs advanced image optimization strategies on its website to ensure fast loading times for its content-rich pages, utilizing lazy loading, responsive images, and performance monitoring.

Resources

Web.dev - Optimize images

article

Squoosh - Image compression

docs

Related Questions

What are Core Web Vitals and why do they matter?

junior
metrics

What is lazy loading and how do you implement it?

junior
loading
Previous
What are Core Web Vitals and why do they matter?
Next
What is lazy loading and how do you implement it?
PrevNext