new PerformanceObserver(callback)
callback<Function>list<PerformanceObserverEntryList>observer<PerformanceObserver>
PerformanceObserver 对象在新的 PerformanceEntry 实例被添加到性能时间线时提供通知。
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.】
callback 会在 PerformanceObserver 被通知有新的 PerformanceEntry 实例时被调用。该回调接收一个 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.】