new PerformanceObserver(callback)


当新的 PerformanceEntry 实例被添加到性能时间线时,则 PerformanceObserver 对象会提供通知。

¥PerformanceObserver objects provide notifications when new PerformanceEntry instances have been added to the Performance Timeline.

import { performance, PerformanceObserver } from 'node:perf_hooks';

const obs = new PerformanceObserver((list, observer) => {
  console.log(list.getEntries());

  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ entryTypes: ['mark'], buffered: true });

performance.mark('test');const {
  performance,
  PerformanceObserver,
} = require('node:perf_hooks');

const obs = new PerformanceObserver((list, observer) => {
  console.log(list.getEntries());

  performance.clearMarks();
  performance.clearMeasures();
  observer.disconnect();
});
obs.observe({ entryTypes: ['mark'], buffered: true });

performance.mark('test');

因为 PerformanceObserver 实例引入了它们自己的额外性能开销,实例不应无限期地订阅通知。一旦不再需要监视器,则用户应立即断开监视器的连接。

¥Because PerformanceObserver instances introduce their own additional performance overhead, instances should not be left subscribed to notifications indefinitely. Users should disconnect observers as soon as they are no longer needed.

PerformanceObserver 接收到有关新的 PerformanceEntry 实例的通知时,则会调用 callback。回调接收到 PerformanceObserverEntryList 实例和对 PerformanceObserver 的引用。

¥The callback is invoked when a PerformanceObserver is notified about new PerformanceEntry instances. The callback receives a PerformanceObserverEntryList instance and a reference to the PerformanceObserver.