PerformanceObserver subscribes to performance entries the browser records: navigation and resource timing, paints, LCP, layout shifts, event timing, long animation frames, and your own performance.mark/measure calls. A RUM pipeline observes with buffered: true, aggregates in the page, sends once when the page is hidden (visibilitychange + sendBeacon or fetch with keepalive), then analyses percentiles segmented by route, device, connection and release. For the Core Web Vitals, use Google's web-vitals library rather than reimplementing their edge cases.
type | What you get |
|---|---|
navigation | TTFB, DNS/TCP/TLS, DOMContentLoaded, load, redirect count, activationStart for prerendered pages |
resource | Per-request timing. Cross-origin detail needs Timing-Allow-Origin on the response |
paint | First Paint, First Contentful Paint |
largest-contentful-paint | LCP candidates, with element and URL |
layout-shift | Shift values and sources (Chromium) |
event | Event Timing, the basis of INP (set durationThreshold) |
long-animation-frame | Slow frames with script attribution (Chromium) |
mark / measure | Your own business timings |
Check PerformanceObserver.supportedEntryTypes before observing.
const metrics = {};
new PerformanceObserver(list => {
const entries = list.getEntries();
metrics.lcp = entries.at(-1).startTime; // latest candidate wins
}).observe({ type: 'largest-contentful-paint', buffered: true });
// Custom, business-relevant timing
performance.mark('search-results-rendered');
performance.measure('search-latency', 'search-submitted', 'search-results-rendered');
addEventListener('visibilitychange', () => {
if (document.visibilityState !== 'hidden') return;
const nav = performance.getEntriesByType('navigation')[0];
const payload = JSON.stringify({
...metrics,
ttfb: nav?.responseStart,
route: location.pathname,
release: window.__RELEASE__,
ect: navigator.connection?.effectiveType, // Chromium only
deviceMemory: navigator.deviceMemory, // Chromium only
});
navigator.sendBeacon('/rum', payload);
});
Why visibilitychange and not unload? unload is unreliable on mobile (apps get killed without it firing) and makes the page ineligible for the back/forward cache. Chrome is phasing unload out. pagehide is an acceptable extra signal. Because a page can be hidden and shown again, make the server tolerate several beacons per page view, or send the final value of each metric once.
pageshow with persisted, and subtract activationStart for prerendered pages. web-vitals handles both.buffered: true.Timing-Allow-Origin.visibilitychange, and why unload is harmful.web-vitals (with attribution) for CWV, and custom marks for business moments such as "time to first search result".Spotted a mistake or a rights concern? Report an issue