Skip to content
intermediate Phase 15 · Performance Optimization and Lazy Loading

Optimizing Images and Assets

Cut transfer weight from static media files.

35m
0 problems
Topic Progress 0%

Responsive Images

Serving responsive image sizes with srcset avoids downloading desktop assets on phones.

Using srcset

<img
  srcset="image-480w.jpg 480w,
          image-800w.jpg 800w,
          image-1200w.jpg 1200w"
  sizes="(max-width: 600px) 480px,
         (max-width: 1000px) 800px,
         1200px"
  src="image-800w.jpg"
  alt="Responsive image">

The browser selects the appropriate image based on the viewport width and device pixel ratio.

Angular CLI Asset Configuration

{
  "assets": [
    "src/favicon.ico",
    "src/assets",
    {
      "glob": "**/*",
      "input": "src/assets/images",
      "output": "/assets/images"
    }
  ]
}

Lazy Loading Images

<img
  src="placeholder.jpg"
  data-src="actual-image.jpg"
  loading="lazy"
  alt="Lazy loaded image">

The loading="lazy" attribute tells the browser to defer loading until the image is near the viewport.

Modern Image Formats

Modern image formats like WebP and AVIF offer significantly better compression than JPEG and PNG.

Format Comparison

Format Compression Browser Support Use For
WebP 25-35% smaller than JPEG 95%+ Photos, general use
AVIF 50% smaller than JPEG 85%+ Photos, high quality
JPEG Baseline 100% Legacy fallback
PNG Lossless 100% Icons, transparency
SVG Vector 100% Icons, logos

Converting Images

# Convert to WebP
cwebp -q 80 input.jpg -o output.webp

# Convert to AVIF
avifenc --quality 80 input.jpg output.avif

# Batch convert with ImageMagick
magick mogrify -format webp *.jpg

Picture Element with Fallbacks

<picture>
  <source srcset="image.avif" type="image/avif">
  <source srcset="image.webp" type="image/webp">
  <img src="image.jpg" alt="Fallback">
</picture>

The browser selects the first supported format.

Asset Budgets

{
  "architect": {
    "build": {
      "configurations": {
        "production": {
          "budgets": [
            {
              "type": "anyComponentStyle",
              "maximumWarning": "2kb",
              "maximumError": "4kb"
            }
          ]
        }
      }
    }
  }
}

Image Optimization Checklist

  • Use WebP/AVIF with JPEG/PNG fallbacks
  • Serve responsive sizes with srcset
  • Lazy load below-the-fold images
  • Compress images (aim for < 200KB per image)
  • Use SVG for icons and logos
  • Set explicit width/height to prevent layout shift

Quiz

1. What does srcset do?

Question 1 options

2. Which modern image format offers the best compression?

Question 2 options

3. What does loading="lazy" do?

Question 3 options

Flashcards

Question

What does srcset do?

Answer

Provides multiple image sizes. Browser selects based on viewport.

Question

Best modern image format?

Answer

AVIF — 50% smaller than JPEG. WebP as fallback.

Question

What does loading="lazy" do?

Answer

Defers loading until image is near viewport.

Revision Notes

Key Takeaways

  • 1. srcset serves responsive image sizes
  • 2. AVIF and WebP offer significant compression savings
  • 3. loading="lazy" defers offscreen images
  • 4. Use the picture element for format fallbacks

Interview Tips

  • • Explain responsive image techniques
  • • Be ready to describe modern image formats
  • • Mention lazy loading and layout shift prevention

Cheat Sheet

Image Optimization Cheat Sheet

Responsive images:

<img srcset="image-480w.jpg 480w, image-800w.jpg 800w"
     sizes="(max-width: 600px) 480px, 800px">

Modern formats:

  • AVIF: best compression
  • WebP: good fallback
  • JPEG/PNG: legacy fallback

Lazy loading:

<img loading="lazy" src="image.jpg">

Continue Learning