Docs
19.5-Performance-Observer
19.5 Performance Observer
Overview
PerformanceObserver provides a way to observe and react to performance measurement events, enabling detailed performance monitoring and optimization.
Performance Entry Types
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā Performance Entry Types ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā navigation ā ā Page load metrics (DNS, TCP, DOM) ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā resource ā ā Resource loading (images, scripts, CSS) ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā mark ā ā Custom timestamps (performance.mark()) ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā measure ā ā Duration between marks (performance.measure()) ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā paint ā ā First Paint, First Contentful Paint ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā longtask ā ā Tasks blocking main thread > 50ms ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā largest-contentful-paint ā ā LCP metric ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā layout-shift ā ā CLS metric ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā first-input ā ā FID metric ā
ā āāāāāāāāāāāāāāāāāāā ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Core Web Vitals
| Metric | Description | Good | Needs Improvement | Poor |
|---|---|---|---|---|
| LCP | Largest Contentful Paint | ⤠2.5s | ⤠4.0s | > 4.0s |
| FID | First Input Delay | ⤠100ms | ⤠300ms | > 300ms |
| CLS | Cumulative Layout Shift | ⤠0.1 | ⤠0.25 | > 0.25 |
| INP | Interaction to Next Paint | ⤠200ms | ⤠500ms | > 500ms |
Basic Usage
// Create observer
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
console.log(entry.name, entry.duration);
}
});
// Start observing
observer.observe({ entryTypes: ['measure', 'mark'] });
// Or observe with buffered option (get past entries)
observer.observe({
type: 'largest-contentful-paint',
buffered: true,
});
// Create custom marks and measures
performance.mark('start');
// ... code to measure ...
performance.mark('end');
performance.measure('operation', 'start', 'end');
Resource Timing Properties
| Property | Description |
|---|---|
startTime | When the resource started loading |
duration | Total time to load the resource |
transferSize | Size of the resource transferred |
decodedBodySize | Decompressed size of the resource |
responseEnd | When the last byte was received |
Summary
- ā¢Non-blocking performance monitoring
- ā¢Access to Core Web Vitals
- ā¢Custom marks and measures
- ā¢Resource loading analysis
- ā¢Long task detection
- ā¢Historical entry access with
buffered: true