Core Web Vitals
Build an accurate mental model of what the browser does, from navigation and HTTP through parsing, rendering, storage, workers, security, and DevTools, so you can predict behavior and debug real pages.
What you will be able to do
#- Define LCP, INP, and CLS and their thresholds.
- Distinguish field data from lab data.
- Measure a metric with PerformanceObserver.
Overview
#Core Web Vitals are field metrics that reflect real user experience.
The three metrics
- Largest Contentful Paint (LCP): when the main content appears. Good is 2.5 s or less.
- Interaction to Next Paint (INP): input latency. Good is 200 ms or less.
- Cumulative Layout Shift (CLS): unexpected movement. Good is 0.1 or less.
Field versus lab
Field data (CrUX, the web-vitals library) comes from real users. Lab data (Lighthouse, the Performance panel) is synthetic and reproducible. Lab is for debugging; field is for judging.
Measuring in code
new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
console.log('LCP', entry.startTime, entry.url);
}
}).observe({ type: 'largest-contentful-paint', buffered: true });
Improving
LCP: optimize the hero image and TTFB. INP: break up long tasks and reduce rendering work. CLS: reserve space for images and fonts with width and height and font-display.
Questions to carry forward
- Why do lab and field disagree?
- Which shifts are excluded from CLS?
- How do you attribute an LCP element?
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
A page reports LCP 3.1 s, INP 180 ms, CLS 0.05. Which metric is outside the good threshold?
Exercise 2Implement
Write a PerformanceObserver that logs CLS entries.
starter.javascriptnew PerformanceObserver((list) => { // TODO }).observe({ type: 'layout-shift', buffered: true });Reveal the reference answer
reference.javascriptnew PerformanceObserver((list) => { for (const entry of list.getEntries()) { if (!entry.hadRecentInput) { console.log('CLS contribution', entry.value); } } }).observe({ type: 'layout-shift', buffered: true });Authored expected output: It logs layout-shift values that were not caused by recent input.
CLS sums layout-shift entries whose hadRecentInput is false, because shifts within 500 ms of user input are excluded.
Common misconceptions
#- Lighthouse scores are the same as field metrics.
- CLS counts layout shifts caused by user interaction.
- INP and FID measure the same thing.
Follow-up practice
#- browser-internals-debugging-capstone
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.