PerformanceObserver: PerformanceObserver() constructor

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

* Some parts of this feature may have varying levels of support.

Note: This feature is available in Web Workers.

The PerformanceObserver() constructor creates a new PerformanceObserver object with the given observer callback. The observer callback is invoked when performance entry events are recorded for the entry types that have been registered, via the observe() method.

Syntax

js
new PerformanceObserver(callback)

Parameters

callback

A PerformanceObserverCallback callback that will be invoked when observed performance events are recorded. When the callback is invoked, the following parameters are available:

entries

The list of performance observer entries.

observer

The observer object that is receiving the above entries.

options

An object with the following properties:

droppedEntriesCount

The number of entries which could not be recorded because the Performance object's internal buffer was full.

Note that this is only provided the first time the observer calls the callback, when the buffered entries are replayed. Once the observer starts making future observations, it no longer needs to use the buffer. After the first time, options will be an empty object ({}).

Return value

A new PerformanceObserver object which will call the specified callback when observed performance events occur.

Examples

Creating a PerformanceObserver

The following example creates a PerformanceObserver watching for "mark" (PerformanceMark) and "measure" (PerformanceMeasure) events. The perfObserver callback provides a list (PerformanceObserverEntryList) which allows you get observed performance entries.

js
function perfObserver(list, observer) {
  list.getEntries().forEach((entry) => {
    if (entry.entryType === "mark") {
      console.log(`${entry.name}'s startTime: ${entry.startTime}`);
    }
    if (entry.entryType === "measure") {
      console.log(`${entry.name}'s duration: ${entry.duration}`);
    }
  });
}
const observer = new PerformanceObserver(perfObserver);
observer.observe({ entryTypes: ["measure", "mark"] });

Dropped buffer entries

You can use PerformanceObserver with a buffered flag to listen to past performance entries. There is a buffer size limit, though. The performance observer callback contains an options object: the first time the observer calls the callback, the options parameter will have a droppedEntriesCount property that tells you how many entries were dropped due to the buffer storage being full. Subsequent callbacks will have an empty options parameter.

js
function perfObserver(list, observer, options) {
  list.getEntries().forEach((entry) => {
    // do something with the entries
  });
  if (options?.droppedEntriesCount > 0) {
    console.warn(
      `${options?.droppedEntriesCount} entries got dropped due to the buffer being full.`,
    );
  }
}

const observer = new PerformanceObserver(perfObserver);
observer.observe({ type: "resource", buffered: true });

Usually, there are a lot of resource timing entries, and for these entries specifically, you can also set a larger buffer using performance.setResourceTimingBufferSize() and watch for the resourcetimingbufferfull event.

Specifications

Specification
Performance Timeline
# dom-performanceobserver-constructor

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
PerformanceObserver() constructor
Chrome – Full support
Chrome 52 (Release date: 2016-07-20)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 57 (Release date: 2017-11-14)
footnote Full support
Opera – Full support
Opera 39 (Release date: 2016-08-02)
footnote Full support
Safari – Full support
Safari 11 (Release date: 2017-09-19)
footnote Full support
Chrome Android – Full support
Chrome Android 52 (Release date: 2016-07-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 57 (Release date: 2017-11-28)
footnote Full support
Opera Android – Full support
Opera Android 41 (Release date: 2016-10-25)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11 (Release date: 2017-09-19)
footnote Full support
Samsung Internet – Full support
Samsung Internet 6 (Release date: 2017-08-23)
footnote Full support
WebView Android – Full support
WebView Android 52 (Release date: 2016-07-27)
footnote Full support
WebView on iOS – Full support
WebView on iOS 11 (Release date: 2017-09-19)
footnote Full support
Bun – Full support
Bun 1.0.22 (Release date: 2024-01-10)
footnote Full support
Deno – Full support
Deno 2.7 (Release date: 2026-02-25)
footnote Full support
Node.js – Full support
Node.js 8.5 (Release date: 2017-09-12)
footnote Full support
droppedEntriesCount() parameter
Experimental
Chrome – Full support
Chrome 95 (Release date: 2021-10-19)
footnote Full support
Edge – Full support
Edge 95 (Release date: 2021-10-21)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 81 (Release date: 2021-11-04)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 95 (Release date: 2021-10-19)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 67 (Release date: 2022-01-31)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 17 (Release date: 2022-05-04)
footnote Full support
WebView Android – Full support
WebView Android 95 (Release date: 2021-10-19)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support
Bun – No support
Bun
footnote No support
Deno – No support
Deno
footnote No support
Node.js – No support
Node.js
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.