PerformanceObserver: takeRecords() 方法

Baseline 已广泛支持

此功能已成熟,并可在多种设备和浏览器版本上运行。自 2021 年 9 月起,所有浏览器均已支持此功能。

注意:此功能在 Web Workers 中可用。

takeRecords() 方法是 PerformanceObserver 接口的一部分,它会返回性能观察器中存储的当前 PerformanceEntry 对象列表,并清空该列表。

语法

js
takeRecords()

参数

无。

返回值

一个 PerformanceEntry 对象列表。

示例

获取记录

下面的示例将性能条目(performance entries)的当前列表存储在 records 中,并清空性能观察器。

js
const observer = new PerformanceObserver((list, obj) => {
  list.getEntries().forEach((entry) => {
    // Process "mark" and "measure" events
  });
});
observer.observe({ entryTypes: ["mark", "measure"] });
const records = observer.takeRecords();
console.log(records[0].name);
console.log(records[0].startTime);
console.log(records[0].duration);

规范

规范
性能时间线
# dom-performanceobserver-takerecords

浏览器兼容性