Skip to the page

Measuring performance · mid

PerformanceObserver, the web-vitals library and sending RUM data

Browsers expose performance entries (navigation, resource, paint, largest-contentful-paint, event, layout-shift, long-animation-frame) through PerformanceObserver, and the web-vitals library turns them into correct LCP, INP and CLS values that you send to analytics when the page is hidden.

The notes

What you must know

  • PerformanceObserver calls your callback asynchronously with batches of entries. Use { type: 'largest-contentful-paint', buffered: true } so you also get entries that happened before your script ran.
  • The web-vitals library (onLCP, onINP, onCLS, onFCP, onTTFB) handles the hard edge cases: background tabs, bfcache restores, prerendering, and reporting at the right time. Version 5 removed onFID.
  • The 'attribution' build of web-vitals tells you WHY: the LCP element and its subparts, the INP target element and its phases, and which element shifted for CLS.
  • Send data when the page becomes hidden (visibilitychange to 'hidden') using navigator.sendBeacon or fetch with keepalive: true. Do not rely on unload: it is unreliable on mobile and blocks bfcache.
  • User Timing (performance.mark / performance.measure) lets you time your own steps, like 'search results rendered', and these marks show up in the DevTools Performance panel.
  • Each metric value is reported per page view; aggregate on the server to p75 by segment.

The red pen

Common traps: mistake → fix

  • Mistake: "Read performance.timing"

    Fix: that API is deprecated; use PerformanceNavigationTiming via performance.getEntriesByType('navigation') or an observer.

  • Mistake: "Send metrics in the unload event"

    Fix: unload often does not fire on mobile and makes the page ineligible for bfcache; use visibilitychange + sendBeacon.

  • Mistake: "The observer callback runs immediately when the entry happens"

    Fix: callbacks are queued and run later, in batches.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you measure Core Web Vitals for real users?
  2. What is PerformanceObserver and what does 'buffered: true' do?
  3. When and how do you send the metrics to your server so you don't lose them?
  4. How would you find which element is the LCP element for most users, not just on your machine?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview