Skip to content

Web Performance

Optimize web performance — loading, rendering, images, and Core Web Vitals.

What you'll learn

  • Why performance matters for user experience and SEO rankings
  • Core Web Vitals: LCP, FID/INP, and CLS — what they measure
  • Techniques: code splitting, lazy loading, image optimization, caching
  • How to measure performance with Lighthouse and the Performance API

Concept

Web Performance

Web performance is how fast your page loads and how smoothly it responds to user input. It matters because users leave slow pages — studies show that a 1-second delay can reduce conversions by 7%. Google also uses performance as a ranking factor, so slow pages rank lower. Performance is not a one-time optimization but an ongoing discipline: measure, optimize, re-measure.

Core Web Vitals

Google's Core Web Vitals are three metrics that summarize the user experience:

  • LCP (Largest Contentful Paint) — when the largest visible element renders. Target: under 2.5 seconds. Measures *loading*.
  • INP (Interaction to Next Paint) — the time from a user interaction to the next visual update. Target: under 200 milliseconds. Measures *interactivity*. (Replaces FID in 2024.)
  • CLS (Cumulative Layout Shift) — how much the layout shifts unexpectedly as the page loads. Target: under 0.1. Measures *visual stability*.

A good score on all three means the page loads fast, responds quickly, and does not jump around. You can see your scores in Chrome DevTools (Lighthouse tab) or in the Chrome User Experience Report.

Loading Optimization

  • Minify and compress — remove whitespace and comments from CSS/JS, and enable gzip or Brotli compression on the server. Brotli is ~20% smaller than gzip.
  • Code splitting — load only the JavaScript needed for the current page. Modern bundlers (Webpack, Vite) split code automatically with dynamic imports.
  • Lazy loading — defer non-critical resources. Use loading="lazy" on images below the fold. Load third-party scripts (analytics, chat widgets) with defer or after page load.
  • Caching — set Cache-Control headers so the browser caches static assets. Use content hashing in filenames so cached files update when content changes.

Image Optimization

Images are usually the largest resources on a page. Optimize them:

  • Use modern formats: WebP (smaller than JPEG/PNG) or AVIF (even smaller).
  • Serve responsive images with <img srcset> so phones get small images and desktops get large ones.
  • Set explicit width and height attributes to prevent layout shift (CLS).
  • Lazy-load images below the fold with loading="lazy".

Rendering Optimization

  • Reduce layout thrashing — avoid reading and writing DOM styles in a loop. Batch reads, then batch writes.
  • Animate only transform and opacity — these do not trigger layout recalculation.
  • Debounce scroll and resize handlers — these fire many times per second. Debounce to run at most once per frame.
  • Use requestAnimationFrame for visual updates to sync with the browser's repaint cycle.

Measuring Performance

Use Lighthouse (in Chrome DevTools) for a comprehensive audit with actionable suggestions. Use the Performance API (performance.now(), PerformanceObserver) for real-user monitoring in production. Track your Core Web Vitals over time — a one-time measurement is not enough; performance regresses as code grows.

Example

              <!-- Responsive, lazy-loaded image with explicit dimensions -->
<img
  src="hero-800.webp"
  srcset="hero-400.webp 400w,
          hero-800.webp 800w,
          hero-1200.webp 1200w"
  sizes="(max-width: 600px) 400px,
         (max-width: 1000px) 800px,
         1200px"
  width="1200"
  height="600"
  loading="lazy"
  decoding="async"
  alt="A scenic mountain landscape"
/>

<!-- Deferred non-critical JavaScript -->
<script src="analytics.js" defer></script>

<!-- Preload the most important resource -->
<link rel="preload" as="image" href="hero-800.webp" />

<!-- Dynamic import for code splitting -->
<script>
  // This chunk loads only when the user clicks
  document.querySelector("#open-editor")
    .addEventListener("click", async () => {
      const { Editor } = await import("./editor.js");
      new Editor().open();
    });
</script>
            

The image uses srcset and sizes for responsive delivery, explicit width/height to prevent CLS, and loading=lazy for deferred loading. The analytics script uses defer so it does not block rendering. The preload hint starts the hero image early. The dynamic import loads the editor code only when needed, splitting the bundle.

Try it

  • SVG Optimizer

    Optimizing SVGs reduces file size and improves load performance — try it.

  • Image Color Picker

    Understanding image data helps you choose efficient formats and compression.

Common mistakes

The mistake

Loading all JavaScript upfront in a single large bundle.

The fix

Use code splitting with dynamic imports so each page loads only the code it needs. Modern bundlers handle this automatically. Lazy-load below-the-fold components and third-party scripts with defer or dynamic import.

The mistake

Serving full-size images and relying on CSS to resize them.

The fix

Use the srcset and sizes attributes to serve appropriately sized images for each device. Use modern formats (WebP, AVIF). Set explicit width and height attributes to prevent layout shift. Lazy-load images below the fold.

Related Cheatsheets

  • CSS Cheatsheet

    Performance-friendly CSS: containment, will-change, and efficient selectors.

Practice Web Performance

4 exercises

Practice Web Performance

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors