-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
65 lines (47 loc) · 2.44 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
---
title: PerformanceObserver
slug: Web/API/PerformanceObserver
page-type: web-api-interface
browser-compat: api.PerformanceObserver
---
{{APIRef("Performance API")}} {{AvailableInWorkers}}
The **`PerformanceObserver`** interface is used to observe performance measurement events and be notified of new {{domxref("PerformanceEntry","performance entries", '', 'true')}} as they are recorded in the browser's _performance timeline_.
## Constructor
- {{domxref("PerformanceObserver.PerformanceObserver","PerformanceObserver()")}}
- : Creates and returns a new `PerformanceObserver` object.
## Static properties
- {{domxref("PerformanceObserver.supportedEntryTypes_static", "PerformanceObserver.supportedEntryTypes")}} {{ReadOnlyInline}}
- : Returns an array of the {{domxref("PerformanceEntry.entryType","entryType")}} values supported by the user agent.
## Instance methods
- {{domxref("PerformanceObserver.observe","PerformanceObserver.observe()")}}
- : Specifies the set of entry types to observe. The performance observer's callback function will be invoked when performance entry is recorded for one of the specified `entryTypes`.
- {{domxref("PerformanceObserver.disconnect","PerformanceObserver.disconnect()")}}
- : Stops the performance observer callback from receiving performance entries.
- {{domxref("PerformanceObserver.takeRecords","PerformanceObserver.takeRecords()")}}
- : Returns the current list of performance entries stored in the performance observer, emptying it out.
## Examples
### Creating a PerformanceObserver
The following example creates a `PerformanceObserver` watching for "mark" ({{domxref("PerformanceMark")}}) and "measure" ({{domxref("PerformanceMeasure")}}) events.
The `perfObserver` callback provides a `list` ({{domxref("PerformanceObserverEntryList")}}) which allows you to 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"] });
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{domxref('MutationObserver')}}
- {{domxref('ResizeObserver')}}
- {{domxref('IntersectionObserver')}}