When to Use
- Building custom telemetry and RUM solutions.
- Measuring Core Web Vitals directly from users in production.
- Detecting Long Tasks that block the main thread and harm INP.
When NOT to Use
- When a high-level library like `web-vitals` solves your problem with less code.
- Running intensive data-processing inside the observer callback itself.
Code Examples
Good: Measuring Long Tasks
Uses PerformanceObserver to detect tasks exceeding 50ms without polling.
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
console.log('Main thread blocked for:', entry.duration, 'ms');
}
});
observer.observe({ type: 'longtask', buffered: true });