Web Development · Module 12 · Lesson 21 min read

Web Performance

Why speed matters

Slow pages lose visitors and rank lower on Google. Mobile users on slow networks feel it most.

Core Web Vitals (Google's key metrics)

MetricMeasuresGood
LCP (Largest Contentful Paint)how fast the main content appears≤ 2.5 s
INP (Interaction to Next Paint)how fast the page responds to taps and clicks≤ 200 ms
CLS (Cumulative Layout Shift)how much things jump around while loading≤ 0.1
Measure them with Lighthouse (DevTools) or PageSpeed Insights.

Biggest wins

Images, usually the heaviest part:

  • modern formats (WebP, AVIF) at the right size, with srcset for different screens,
  • loading="lazy" below the fold,
  • width and height set to prevent layout shift.

JavaScript:

  • ship less (avoid huge libraries for small tasks),
  • defer scripts, and split code with dynamic import(),
  • avoid long tasks that block taps.

CSS and fonts:

  • remove unused CSS, keep critical CSS small,
  • font-display: swap, and fewer font weights.

Network:

  • compression (gzip or Brotli), HTTP caching headers,
  • a CDN to serve files from near the user,
  • fewer, smaller requests.

💡 Measure before and after each change. Optimise what the numbers say, not what you guess.

Example

HTML
<!-- Performance-friendly image and script loading -->
<img
  src="photo-800.webp"
  srcset="photo-400.webp 400w, photo-800.webp 800w, photo-1600.webp 1600w"
  sizes="(max-width: 600px) 100vw, 800px"
  width="800" height="450"
  alt="Team working together"
  loading="lazy"
  decoding="async" />

<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<script src="analytics.js" async></script>
<script src="app.js" type="module"></script>

<script type="module">
  // Load a heavy library only when it's needed
  document.querySelector("#chart-btn")?.addEventListener("click", async () => {
    const { drawChart } = await import("./chart.js");
    drawChart();
  });
</script>