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.
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:
| Metric | Measures | Good |
|---|---|---|
| LCP — Largest Contentful Paint | Loading: when the main content (hero image, headline) appears | ≤ 2.5 s |
| INP — Interaction to Next Paint | Responsiveness: delay from a click/tap/keypress to the next frame, across the visit | ≤ 200 ms |
| CLS — Cumulative Layout Shift | Visual 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) | |
|---|---|---|
| Tools | Lighthouse, WebPageTest, DevTools Performance panel | Chrome UX Report, web-vitals library, RUM vendors |
| Pros | Reproducible, detailed traces, good for debugging | Real devices, networks and user behaviour |
| Cons | One device and network profile | Noisy, less diagnostic detail |
Use field data to find what is slow for real users, and lab tools to find why.
Where time goes
Improving LCP (loading)
- Fast TTFB: cache HTML at a CDN (static/ISR pages — Rendering strategies), optimise slow queries, keep servers close to data.
- Discover the LCP resource early: make it a real
<img>in the HTML (not a CSS background or JS-inserted), addfetchpriority="high", and never lazy-load it. - Shrink it: modern formats (AVIF, WebP), correct dimensions with
srcset/sizes, compression. - Remove render-blocking resources: small critical CSS,
deferscripts (Browser rendering). - Reduce connection setup:
preconnectto 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()orsetTimeout, process in chunks. - Do the minimum in event handlers: update the UI first, defer non-urgent work; in React,
useTransitionfor 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
widthandheight(oraspect-ratio) on images, videos and iframes so space is reserved. - Reserve space for ads, embeds and late-loading banners.
- Fonts:
font-display: optionalorswapwith 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, nottop/height.
Caching
The fastest request is the one never made.
# 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| Layer | What it caches |
|---|---|
| Browser HTTP cache | Assets per Cache-Control / ETag |
| Service worker | Programmable offline cache (PWAs) |
| CDN | Static files and cacheable HTML/API responses at edge locations near users (Caching) |
| Server/app cache | Rendered 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 → nativeIntl/ 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.
<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.
preloadcritical late-discovered resources;prefetchlikely 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.
- 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.