Lighthouse on your laptop says LCP is 1.2 seconds. Real-user data says LCP is 3.8 seconds. Same page. So which one is wrong?
The answer is easy once you know where each number comes from. Measuring performance means getting real numbers: how many milliseconds the page needs, which file is the slowest, how many kilobytes get downloaded. Without numbers, "it feels fast enough" is just a feeling. It's like a bathroom scale: if you diet without ever weighing yourself, you can't tell whether it's working.
Lab data and field data
- Lab data is measured in a controlled simulation: one device, one imitated network, results you can repeat. An example is Lighthouse, the audit tool built into Chrome DevTools that PageSpeed Insights also uses. It's good for finding the cause of a problem before you deploy.
- Field data is collected from real users, each with their own phone, network and way of using the page. An example is CrUX (the Chrome User Experience Report, statistics from Chrome users who opted in to share data), which PageSpeed Insights shows. You can also collect it yourself with the
web-vitalslibrary. That's called RUM, real user monitoring.
So neither of the two numbers at the top is wrong. Your laptop is fast, while many of your users have slower phones. To judge what users experience, field data is the reference. Lab data is for finding the cause. One more thing: INP needs real clicks, so Lighthouse can't measure it. Lighthouse shows TBT (Total Blocking Time) instead.
Measuring your own code
The browser has a performance object. With mark and measure, you can measure how long a piece of code runs:
performance.mark("sort-start");
numbers.sort((a, b) => a - b); // the code to measure
performance.mark("sort-end");
performance.measure("sort", "sort-start", "sort-end");
const [result] = performance.getEntriesByName("sort");
console.log(result.duration); // say 23.4 (milliseconds)
performance.mark(name)records one point in time under that name.performance.measure(name, start, end)records the gap between two marks.getEntriesByName("sort")returns an array, and itsdurationis in milliseconds. These marks and measures also show up in the Chrome DevTools Performance panel.
Resource Timing: every downloaded file
performance.getEntriesByType("resource") gives one entry for every file the page downloaded: CSS, JavaScript, images, fonts. The three most useful fields:
name: the file's URL.duration: how many milliseconds from sending the request to receiving the last byte.transferSize: how many bytes crossed the network. It's0when the file came from the cache, or when a file from another domain doesn't allow its numbers to be read (through theTiming-Allow-Originheader).
const data = performance.getEntriesByType("resource").map((e) => ({
name: e.name,
duration: e.duration,
transferSize: e.transferSize,
}));
Summing the numbers up
Lots of entries are easier to read as a summary. Take these three entries (the Lab and the exercise use them too):
function analyzeTimings(timings) {
let totalDuration = 0;
let totalBytes = 0;
let slowest = timings[0];
for (const t of timings) {
totalDuration += t.duration;
totalBytes += t.transferSize;
if (t.duration > slowest.duration) slowest = t;
}
return {
totalDuration,
slowestResource: slowest.name,
totalTransferKB: Math.floor(totalBytes / 1024),
count: timings.length,
};
}
const data = [
{ name: "style.css", duration: 120, transferSize: 15360 },
{ name: "app.js", duration: 340, transferSize: 102400 },
{ name: "logo.png", duration: 80, transferSize: 5120 },
];
console.log(analyzeTimings(data));
// { totalDuration: 540, slowestResource: "app.js", totalTransferKB: 120, count: 3 }
| Pass | t.name |
totalDuration |
totalBytes |
slowest |
|---|---|---|---|---|
| 1 | style.css | 120 | 15,360 | style.css |
| 2 | app.js | 460 | 117,760 | app.js |
| 3 | logo.png | 540 | 122,880 | app.js |
sloweststarts as the first item, and is replaced whenever a biggerdurationcomes along.- 1 KB here is 1,024 bytes. 122,880 / 1,024 = 120, and
Math.floorrounds down when the result has a fraction. totalDurationis the sum of every duration, not how long the page took to load. Files download at the same time, so the page itself finishes sooner than 540 ms.- The order of the keys in the returned object is checked in the exercise:
totalDuration,slowestResource,totalTransferKB, thencount.
💡 Core Web Vitals from real users
The web-vitals library (made by the Chrome team) measures LCP, INP and CLS in your visitors' browsers:
import { onLCP, onINP, onCLS } from "web-vitals";
function report(metric) {
console.log(metric.name, metric.value, metric.rating); // say "LCP" 2180 "good"
}
onLCP(report);
onINP(report);
onCLS(report);
metric.rating is "good", "needs-improvement" or "poor". In a real app, swap console.log for navigator.sendBeacon(...) to send it to your analytics server. Under the hood, the library uses PerformanceObserver, which tells you whenever a new entry arrives:
new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
console.log("LCP candidate:", entry.startTime, entry.element);
}
}).observe({ type: "largest-contentful-paint", buffered: true });
⚠️ Numbers that are easy to misread
- Trusting one Lighthouse run on a fast laptop. That's lab data from one device. Check field data to know what users feel.
- Dividing by 1000, or using
Math.round. In the exercise, KB = bytes divided by 1024, rounded down withMath.floor. - Keys in a different order. The exercise compares the result as JSON text, so
{ count, totalDuration, ... }counts as different from{ totalDuration, ..., count }. - Thinking
transferSize: 0means an empty file. Usually the file came from the cache.
TL;DR: Lab data (Lighthouse) is measured in a controlled simulation and is good for finding causes. Field data (CrUX, web-vitals) comes from real users and is the reference for what they experience. Measure your code with performance.mark and performance.measure, and read each file through performance.getEntriesByType("resource") (name, duration, transferSize). To sum them up: add the durations, find the biggest duration, add the bytes and divide by 1024 with Math.floor, and count the entries, with the keys in the order totalDuration, slowestResource, totalTransferKB, count.
⚡ Lab data and field data
Guess first: you want to measure your site in a simulation, straight from Chrome. Which tool?
Lighthouse. That's lab data: one device, one imitated network, results you can repeat. It's good for finding the cause of a problem.
Meaning: Field data comes from real users, such as CrUX or the web-vitals library. It's the reference for what they experience. INP needs real clicks, so Lighthouse shows TBT instead.
⚡ Measure your own code
Write:
performance.mark("sort-start");
numbers.sort((a, b) => a - b);
performance.mark("sort-end");
performance.measure("sort", "sort-start", "sort-end");
Meaning: mark records one point in time, measure records the gap between two marks. Its duration is in milliseconds.
⚡ Sum up every file
Write:
totalTransferKB: Math.floor(totalBytes / 1024),
Meaning: performance.getEntriesByType("resource") gives one entry per file, with name, duration and transferSize. In the exercise, 1 KB = 1,024 bytes, rounded down, and the keys come in the order totalDuration, slowestResource, totalTransferKB, count.