performanceObserver.observe(options)


  • options <Object>
    • entryTypes <string[]> 标识观察者感兴趣的 PerformanceEntry 实例类型的字符串数组。 如果未提供,将抛出错误。
    • buffered <boolean> 如果为 true,则通知回调将使用 setImmediate() 调用,多个 PerformanceEntry 实例通知将在内部缓冲。 如果为 false,则通知将是即时和同步的。 默认值: false

PerformanceObserver 实例订阅由 options.entryTypes 标识的新 PerformanceEntry 实例的通知。

options.bufferedfalse 时,则 callback 会为每个 PerformanceEntry 实例调用一次:

const {
  performance,
  PerformanceObserver
} = require('perf_hooks');

const obs = new PerformanceObserver((list, observer) => {
  // 同步地调用 3 次。`list` 包含一项。
});
obs.observe({ entryTypes: ['mark'] });

for (let n = 0; n < 3; n++)
  performance.mark(`test${n}`);
const {
  performance,
  PerformanceObserver
} = require('perf_hooks');

const obs = new PerformanceObserver((list, observer) => {
  // 调用一次。`list` 包含三个条目。
});
obs.observe({ entryTypes: ['mark'], buffered: true });

for (let n = 0; n < 3; n++)
  performance.mark(`test${n}`);
  • options <Object>
    • entryTypes <string[]> An array of strings identifying the types of PerformanceEntry instances the observer is interested in. If not provided an error will be thrown.
    • buffered <boolean> If true, the notification callback will be called using setImmediate() and multiple PerformanceEntry instance notifications will be buffered internally. If false, notifications will be immediate and synchronous. Default: false.

Subscribes the PerformanceObserver instance to notifications of new PerformanceEntry instances identified by options.entryTypes.

When options.buffered is false, the callback will be invoked once for every PerformanceEntry instance:

const {
  performance,
  PerformanceObserver
} = require('perf_hooks');

const obs = new PerformanceObserver((list, observer) => {
  // Called three times synchronously. `list` contains one item.
});
obs.observe({ entryTypes: ['mark'] });

for (let n = 0; n < 3; n++)
  performance.mark(`test${n}`);
const {
  performance,
  PerformanceObserver
} = require('perf_hooks');

const obs = new PerformanceObserver((list, observer) => {
  // Called once. `list` contains three items.
});
obs.observe({ entryTypes: ['mark'], buffered: true });

for (let n = 0; n < 3; n++)
  performance.mark(`test${n}`);