PerformanceObserver & RUM Telemetry
PerformanceObserver subscribes to the browser's internal performance timeline without polling performance.getEntries(). Pass buffered: true to capture entries recorded before your script booted, and flush metrics via navigator.sendBeacon() on visibilitychange so you never break bfcache.
RUM Telemetry: Buffered Observe to sendBeacon Flush
Trace how PerformanceObserver captures retroactive entries and flushes on visibilitychange without blocking bfcache.
Observer attaches after hydration and reads LCP/longtask entries already recorded during boot.
obs.observe({ type: 'largest-contentful-paint', buffered: true });- bufferedEntriesRead
- 3
- mainThreadBlocked
- false
- bfcacheEligible
- true
Under the Hood
Polling performance.getEntriesByType() on a timer is like checking your mailbox every ten milliseconds; PerformanceObserver acts like a push notification delivered in an idle callback whenever the engine records a Core Web Vitals entry or Long Animation Frame (long-animation-frame).
- Calling
observer.observe({ type, buffered: true })replays entries emitted before the observer attached. - Each
PerformanceEntrycarries high-resolutionDOMHighResTimeStampoffsets and attribution fields (such as the LCPelementor blocking script URL). - On
document.visibilityState === 'hidden',navigator.sendBeacon()hands the payload to the browser's background network queue without blocking navigation or invalidating bfcache.
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
queueMetric({ name: entry.name, start: entry.startTime, value: entry.duration });
}
});
observer.observe({ type: 'long-animation-frame', buffered: true });
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') flushWithSendBeacon();
});Problem Genealogy
- 2012 (Navigation Timing
performance.timing): Exposed integer epoch timestamps (domComplete - navigationStart), forcing manual polling. - 2016 (
PerformanceObserver): Introduced push-based observation and sub-millisecond monotonic clocks (performance.now()). - 2024+ (Long Animation Frames / LoAF): Replaced opaque 50 ms
longtaskwarnings with full script-URL attribution showing which third-party script froze the frame.
Production Pitfalls
- Forgetting
buffered: true: Attaching an observer after React/Vue hydrates misses early LCP and FCP entries unlessbuffered: trueis set. - Flushing on
unload: Usingwindow.addEventListener('unload', ...)disables bfcache across your entire domain; always usevisibilitychangeorpagehide.
- When not to use 100% raw RUM beacons: On high-traffic sites, sending every
resourceentry for every pageview inflates analytics bills and user battery; sample 1–5% of sessions or aggregate before flushing.
Connections & Logical Next Steps
PerformanceObserver is the low-level browser API that emits LCP, CLS, and Event Timing entries.
RUM scripts must flush on visibilitychange/pagehide instead of unload to preserve bfcache.
PerformanceObserver longtask and LoAF entries measure the main-thread stalls that scheduler.yield() breaks up.