Web Performance

Measuring and improving speed — Core Web Vitals (LCP, INP, CLS), lab vs field data, reducing bytes and requests, caching and CDNs, images and fonts, JavaScript cost, code splitting, and a practical optimisation checklist.

Intermediate 4 min readLesson 13 of 14

Speed is a feature: faster pages convert better, rank better and work for users on slow phones and networks. Performance work follows one loop — measure, find the bottleneck, fix, measure again.

Core Web Vitals

Google's user-centric metrics, measured at the 75th percentile of real visits:

MetricMeasuresGood
LCP — Largest Contentful PaintLoading: when the main content (hero image, headline) appears≤ 2.5 s
INP — Interaction to Next PaintResponsiveness: delay from a click/tap/keypress to the next frame, across the visit≤ 200 ms
CLS — Cumulative Layout ShiftVisual stability: how much content jumps around unexpectedly≤ 0.1

Supporting metrics: TTFB (time to first byte — server and network), FCP (first contentful paint), TBT (total blocking time — lab proxy for INP).

Lab vs field data

Lab (synthetic)Field (RUM)
ToolsLighthouse, WebPageTest, DevTools Performance panelChrome UX Report, web-vitals library, RUM vendors
ProsReproducible, detailed traces, good for debuggingReal devices, networks and user behaviour
ConsOne device and network profileNoisy, less diagnostic detail

Use field data to find what is slow for real users, and lab tools to find why.

Where time goes

DNS+TCP+TLSTTFB (server)HTML+CSSHero imageJS + hydrate00.30.81.22.23

Improving LCP (loading)

  1. Fast TTFB: cache HTML at a CDN (static/ISR pages — Rendering strategies), optimise slow queries, keep servers close to data.
  2. Discover the LCP resource early: make it a real <img> in the HTML (not a CSS background or JS-inserted), add fetchpriority="high", and never lazy-load it.
  3. Shrink it: modern formats (AVIF, WebP), correct dimensions with srcset/sizes, compression.
  4. Remove render-blocking resources: small critical CSS, defer scripts (Browser rendering).
  5. Reduce connection setup: preconnect to critical third-party origins, HTTP/2 or HTTP/3 (UDP and QUIC).

Improving INP (responsiveness)

The main thread can't respond to input while running a long task.

  • Ship less JavaScript: it must be downloaded, parsed, compiled and executed — on a mid-range phone that's several times slower than a laptop.
  • Break up long tasks (> 50 ms): yield to the browser with await scheduler.yield() or setTimeout, process in chunks.
  • Do the minimum in event handlers: update the UI first, defer non-urgent work; in React, useTransition for expensive state updates.
  • Move heavy computation to Web Workers.
  • Avoid layout thrashing and huge DOMs; virtualise long lists (render only visible rows).
  • Audit third-party scripts (tag managers, chat widgets, ads) — often the biggest culprits.

Improving CLS (stability)

  • Always set width and height (or aspect-ratio) on images, videos and iframes so space is reserved.
  • Reserve space for ads, embeds and late-loading banners.
  • Fonts: font-display: optional or swap with a metric-matched fallback (size-adjust) to avoid reflow when the web font loads.
  • Don't insert content above existing content unless the user asked for it; animate with transform, not top/height.

Caching

The fastest request is the one never made.

HTTP
# Fingerprinted static assets (app.3f9c2a.js): cache "forever"
Cache-Control: public, max-age=31536000, immutable

# HTML: always revalidate (cheap 304 if unchanged)
Cache-Control: no-cache
ETag: "v42"

# API response that can be slightly stale
Cache-Control: public, max-age=60, stale-while-revalidate=300
LayerWhat it caches
Browser HTTP cacheAssets per Cache-Control / ETag
Service workerProgrammable offline cache (PWAs)
CDNStatic files and cacheable HTML/API responses at edge locations near users (Caching)
Server/app cacheRendered pages, query results (Redis)

Fingerprint asset filenames with a content hash so you can cache them immutably and bust caches by changing the name.

JavaScript cost

  • Code splitting: load route-level chunks on demand (import(), React.lazy, framework routing does this automatically).
  • Tree shaking: ES modules let bundlers drop unused exports — import only what you use (import debounce from "lodash/debounce").
  • Analyse bundles (webpack-bundle-analyzer, source-map-explorer) and replace heavy libraries (moment.js → native Intl / date-fns).
  • Minify and compress with Brotli (or gzip).
  • Prefer server rendering / Server Components for content so less code ships (Rendering strategies).
  • Load third-party scripts async/defer, after interaction, or in a worker (Partytown).

Images and fonts

Images are usually the largest bytes on a page.

HTML
<img src="product-800.avif"
     srcset="product-400.avif 400w, product-800.avif 800w, product-1600.avif 1600w"
     sizes="(max-width: 640px) 100vw, 640px"
     width="800" height="800" alt="Blue running shoe, side view"
     loading="lazy" decoding="async">

Fonts: self-host WOFF2, subset to needed characters, preload the one or two critical files, limit weights, consider variable fonts or system font stacks.

Network and delivery

  • HTTP/2 and HTTP/3 multiplex many requests on one connection — bundling everything into one huge file matters less than it used to; prefer sensible chunking.
  • preload critical late-discovered resources; prefetch likely next navigations; Speculation Rules to prerender likely next pages.
  • Compress text responses (Brotli), and serve from a CDN near users.
  • 103 Early Hints lets the server tell the browser about critical resources while still generating HTML.

Performance budgets and culture

Set budgets (e.g. "≤ 170 KB of compressed JS on the critical path", "LCP ≤ 2.5 s on a mid-range Android over 4G") and enforce them in CI with Lighthouse CI or bundle-size checks. Regressions creep in one dependency at a time.

Key takeaways
  • Core Web Vitals: LCP (loading ≤ 2.5 s), INP (responsiveness ≤ 200 ms), CLS (stability ≤ 0.1); use field data to prioritise, lab tools to debug.
  • LCP: fast TTFB, early-discovered and prioritised hero image, no render-blocking resources.
  • INP: ship and run less JavaScript, break up long tasks, use workers, tame third parties.
  • CLS: reserve space for media, ads and fonts.
  • Cache aggressively: immutable fingerprinted assets, revalidated HTML, CDNs.
  • Code-split, tree-shake, compress, optimise images and fonts, and enforce performance budgets.