Lesson 27 of 28
In progress
Performance: measure, then fix
Read LCP, CLS and INP, find the slow request or image, and make changes you can prove with a before-and-after measurement.
Performance work is measurement followed by a change, then measurement again. Without the first and last step, it is just churn.
What you will learn
- Read the three Core Web Vitals.
- Find the resource responsible for a slow load.
- Apply the fixes that matter most for a content site.
The three numbers
- LCP (Largest Contentful Paint) measures when the main content appears. Under 2.5 seconds is good.
- CLS (Cumulative Layout Shift) measures unexpected movement. Under 0.1 is good.
- INP (Interaction to Next Paint) measures responsiveness to clicks and typing. Under 200 milliseconds is good.
Each has a different cause. LCP is usually an image, a font or a slow server. CLS is usually an image without dimensions or an ad inserted above content. INP is usually long JavaScript tasks blocking the main thread.
Measure in the browser
The Performance panel records a trace: network, main thread, layout and paint. Record a page load, then look for the longest bars. A long yellow block is JavaScript; a purple block is layout; a green one is painting.
The Network panel sorted by size shows what dominates the download. Sort by time to see what arrives last and delays rendering. Check whether the LCP image is discovered late, which often means it is set by JavaScript or lazy-loaded by mistake.
Lighthouse gives a scored summary and specific opportunities. Treat the score as a pointer, not a grade: the trace explains why.
Images
Images are usually the largest payload.
- Serve the right size; do not send 2000 pixels for a 400-pixel slot.
- Use WebP or AVIF.
- Set
widthandheightso space is reserved. - Lazy-load below the fold, and never lazy-load the hero.
- Add
fetchpriority="high"to the LCP image.
<img src="hero-960.webp" alt="Students working together"
width="960" height="504" fetchpriority="high">
That one tag addresses LCP and CLS at once: the browser knows the size, can prioritise the file, and reserves the space.
Fonts and CSS
A webfont can block text or cause a swap that shifts layout. Self-host the file, preload the one weight used above the fold, and set font-display: swap with a metric-compatible fallback.
CSS in the head blocks rendering, which is correct, but a large stylesheet delays first paint. Keep one file, remove unused rules, and avoid @import, which adds a sequential request.
JavaScript
JavaScript delays interactivity, so the cheapest fix is often less of it.
- Use
deferfor scripts that need the DOM. - Split code so a page does not load features it never uses.
- Avoid long synchronous work; break it up or move it to a worker.
- Remove libraries that duplicate a few lines of platform code.
<script src="app.js" defer></script>
Long tasks also hurt INP. If a click handler does heavy work, the interface freezes until it finishes. Measure with a trace, then reduce the work or schedule it after the paint.
Caching and delivery
Static assets should be cached with a long lifetime and a versioned filename. HTML should be short-lived so updates appear. Compression, usually Brotli, is standard on modern hosts.
A content delivery network helps users far from the server, but it cannot fix a 4 MB hero image. Fix the payload first.
A before-and-after example
Suppose LCP is 4.2 seconds and the trace shows the hero image starting after the CSS and a script. The change:
- Resize the hero to 960×504 and convert to WebP.
- Add width, height and
fetchpriority="high". - Remove
loading="lazy"from that image. - Defer the script that was blocking discovery.
Measure again on the same connection profile. The improvement should be visible in the trace, not just the score.
Adding loading="lazy" to every image, including the hero, delays the largest element and makes LCP worse. Lazy loading is for images below the fold; the first visible image should load eagerly and be prioritised.
Take a page with a hero image, a webfont and one script. Write down which of the three is most likely to delay LCP, and list two changes that would reduce it without removing content.
Show solution
The hero image is usually the LCP element on a content page. Two changes: serve it at the displayed size in WebP with explicit width and height, and give it fetchpriority="high" while removing any lazy loading. Deferring the script helps discovery if it was competing for bandwidth.
A font can delay text if the page’s main content is a heading rather than an image; in that case preloading the used weight and using font-display: swap is the fix. The right answer comes from the trace, which shows which resource actually delayed the paint.
Timing a block of code
Editable. Runs in your browser.
let total = 0;
for (let i = 0; i < 100000; i += 1) total += i;
const elapsed = performance.now() – start;
console.log("loop took", Math.round(elapsed), "ms for total", total);
Press Run to see the output.
performance.now() is a high-resolution timer, which makes it suitable for measuring a section of code. Measure several times and compare medians; a single run is dominated by noise.
Recap
- LCP, CLS and INP point at different causes; the trace shows which applies.
- Size and prioritise images, preload one font weight, and keep CSS and JS small.
- Measure before and after every change, on the same connection profile.
You have finished the performance lesson. Next is the capstone, which brings the whole track together.
Is a perfect Lighthouse score the goal?
Does deferring everything help?
Progress is saved in this browser only. No account, no email.