Front-End Performance and Core Web Vitals
Make pages load fast and feel smooth: what users and search engines measure (LCP, INP, CLS), how the browser renders a page, optimising images, fonts, CSS and JavaScript, caching and CDNs, and how to measure before and after you change anything.
1. Speed is a feature
Visitors leave slow pages. Search engines use page experience as a ranking signal. On a mid-range phone and a mobile network, which is how much of your audience will see the page, every unnecessary kilobyte and every blocked second costs people and money. Performance is also accessibility: slow sites exclude people on cheap devices and expensive data.
2. What to measure: Core Web Vitals
Google’s Core Web Vitals describe real user experience in three numbers:
| Metric | Measures | Good |
|---|---|---|
| LCP (Largest Contentful Paint) | How fast the main content appears | 2.5 s or less |
| INP (Interaction to Next Paint) | How quickly the page responds when someone taps, clicks or types | 200 ms or less |
| CLS (Cumulative Layout Shift) | How much content jumps around while loading | 0.1 or less |
Other useful numbers: TTFB (time to first byte, which depends on the server and network), FCP (first content painted) and total blocking time.
Lab data (Lighthouse in DevTools, run on your machine) is great for debugging. Field data (real users: the Chrome User Experience Report, PageSpeed Insights, web-vitals library, your analytics) shows what people actually get. Measure both, and test on a throttled, mid-range device, not only your fast laptop.
3. How a page loads
- The browser requests the HTML (TTFB is how long that takes to start).
- It parses the HTML and discovers CSS, JavaScript, images and fonts, and requests them.
- CSS blocks rendering: the browser will not paint until it has the CSS it needs.
- Synchronous JavaScript blocks parsing: a plain
<script>pauses the HTML parser while it downloads and runs. - It builds the DOM and the style rules, calculates layout, paints and composites.
- After that, JavaScript can change things, and every change may trigger layout and paint again.
Most optimisation comes down to sending less, sending it sooner, and not blocking the steps above.
4. Images: usually the biggest win
Images are often most of a page’s weight.
- Use modern formats: WebP or AVIF are far smaller than JPEG and PNG at the same quality. Use SVG for logos and icons.
- Resize images to the size they are displayed at (and use
srcsetandsizesfor different screens). Serving a 4000px photo into a 400px slot wastes bytes. - Compress with a build tool or service. 80% quality is usually indistinguishable.
- Reserve space with
widthandheightattributes (or CSSaspect-ratio) to avoid layout shift. - Lazy-load below-the-fold images:
<img loading="lazy" ...>. Do not lazy-load the hero or largest image; give itfetchpriority="high"instead, and preload it if needed. - Never give a file the wrong extension: a PNG renamed
.jpgis still a PNG. Convert properly.
<img src="hero-800.webp"
srcset="hero-480.webp 480w, hero-800.webp 800w, hero-1600.webp 1600w"
sizes="(min-width: 60rem) 50vw, 100vw"
width="800" height="450" alt="Students at a network lab"
fetchpriority="high">
5. Fonts
Web fonts block or shift text if handled badly.
- Use as few families and weights as possible; prefer variable fonts; use WOFF2.
- Self-host (or preconnect to the provider) and add
font-display: swapso text shows immediately in a fallback font. - Preload the one or two critical font files:
<link rel="preload" as="font" type="font/woff2" href="/fonts/inter.woff2" crossorigin>. - Choose a system font stack (
system-ui, sans-serif) when brand allows: zero download.
6. CSS
- Ship only the CSS a page needs; remove unused rules and large frameworks you barely use.
- Inline a small amount of critical CSS for the above-the-fold content and load the rest without blocking, if your build tool supports it.
- Prefer CSS over JavaScript for animation, and animate
transformandopacity, which the compositor handles cheaply. Animatingwidth,topormargintriggers layout on every frame. - Avoid
@importchains in CSS (each waits for the last).
7. JavaScript
JavaScript is the most expensive byte on the web, because it must be downloaded, parsed and executed.
- Send less: audit dependencies with a bundle analyser, drop heavy libraries for small ones (or the platform), and delete dead code. Tree-shaking removes unused exports.
- Defer it: use
<script defer>(runs after parsing, in order) ortype="module"(deferred by default);asyncfor independent scripts like analytics. Never leave large synchronous scripts in<head>. - Split it: code splitting loads each route’s code only when needed (
import('./chart.js')on demand). - Keep the main thread free: break up long tasks (over 50 ms), avoid heavy work in event handlers, debounce input and scroll handlers, and move expensive computation to a Web Worker.
- Do less rendering: batch DOM updates, avoid forcing layout in loops (reading
offsetHeightright after writing styles), and in frameworks avoid re-rendering large trees unnecessarily. - Ship less to the browser: render on the server or at build time where you can (static and server-rendered HTML), and hydrate only the interactive parts. This is the idea behind “islands” in frameworks such as Astro.
8. Network, caching and CDNs
- Compress text (gzip or, better, Brotli) for HTML, CSS and JavaScript. Most hosts and CDNs do it for you; check the
Content-Encodingheader. - Use HTTP caching: long
Cache-Control: public, max-age=31536000, immutablefor fingerprinted files (app.3f9c1.js), and short or revalidated caching for HTML. - Put static files on a CDN, which serves them from a location near the visitor.
- Use HTTP/2 or HTTP/3 (multiplexing makes many small files cheaper), and
preconnectto third-party origins you must use. - Preload the critical resource the browser would discover late (the LCP image, a key font), and avoid long chains of requests that depend on each other.
- Reduce third-party scripts: each tag manager, widget or analytics script competes with your own code.
9. A repeatable process
- Measure a baseline: Lighthouse on a throttled mobile profile, plus field data if you have it. Note LCP, INP, CLS and total bytes.
- Find the biggest problem in the report: the largest image, a render-blocking script, a long task, a layout shift.
- Change one thing.
- Measure again. Keep it if it helped; revert it if not.
- Set a performance budget (for example “under 200 KB of JavaScript, LCP under 2.5 s”) and check it automatically in your build or CI, so performance does not quietly regress.
10. Quick wins checklist
- Hero image: correct size and format,
fetchpriority="high", not lazy. - Other images: modern format,
widthandheight,loading="lazy". - Fonts: WOFF2,
font-display: swap, few weights. - Scripts:
defer, split by route, no unused libraries. - Compression on, caching headers set, files on a CDN.
- Animations use
transformandopacity. - Tested on a throttled mid-range phone.
11. What you learned
You know what the Core Web Vitals mean and how the browser renders a page, so you can aim optimisation at what matters: smaller, correctly sized images, careful fonts, lean and deferred JavaScript, good caching and a CDN. Above all, you measure before and after. Next: the tools that professional front-end teams use every day.
🧪 Want to practise this on your own computer? See how to set up your lab →