performanceObserver.observe(options)
options<Object>type<string> 单个 <PerformanceEntry> 类型。如果已经指定了entryTypes,则不能再提供此项。entryTypes<string[]> 一个字符串数组,用于识别观察者感兴趣的 <PerformanceEntry> 实例类型。如果未提供,将抛出错误。buffered<boolean> 如果为 true,观察者回调将使用全局PerformanceEntry缓冲条目的列表调用。如果为 false,则只会将时间点之后创建的PerformanceEntry发送给观察者回调。默认值:false。
将 <PerformanceObserver> 实例订阅到新 <PerformanceEntry> 实例的通知,这些实例可以通过 options.entryTypes 或 options.type 标识:
【Subscribes the <PerformanceObserver> instance to notifications of new
<PerformanceEntry> instances identified either by options.entryTypes
or options.type:】
import { performance, PerformanceObserver } from 'node:perf_hooks';
const obs = new PerformanceObserver((list, observer) => {
// Called once asynchronously. `list` contains three items.
});
obs.observe({ type: 'mark' });
for (let n = 0; n < 3; n++)
performance.mark(`test${n}`);const {
performance,
PerformanceObserver,
} = require('node:perf_hooks');
const obs = new PerformanceObserver((list, observer) => {
// Called once asynchronously. `list` contains three items.
});
obs.observe({ type: 'mark' });
for (let n = 0; n < 3; n++)
performance.mark(`test${n}`);