Measuring Performance

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…

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

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)

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:

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

💡 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

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.

What you will learn