Performance: getEntries() 方法

Baseline 已广泛支持

此功能已成熟,可跨多种设备和浏览器版本使用。自 2017 年 9 月以来,它已在浏览器中提供。

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

getEntries() 方法返回一个包含当前性能时间线中所有 PerformanceEntry 对象的数组。

如果您只对特定类型或特定名称的性能条目感兴趣,请参阅 getEntriesByType()getEntriesByName()

注意:此方法不会通知您新的性能条目;您只会获得在调用此方法时性能时间轴中存在的条目。要接收有关可用条目的通知,请使用 PerformanceObserver

以下条目类型完全不受此方法支持,即使可能存在这些类型的条目也不会返回

要访问这些类型的条目,您必须改用 PerformanceObserver

语法

js
getEntries()

参数

无。

返回值

一个包含 PerformanceEntry 对象的 Array。这些条目将按照其 startTime 的时间顺序排列。

示例

记录所有性能标记和度量

假设您在代码的适当位置创建了自己的 PerformanceMarkPerformanceMeasure 对象,您可能会像这样将它们全部记录到控制台:

js
// Example markers/measures
performance.mark("login-started");
performance.mark("login-finished");
performance.mark("form-sent");
performance.mark("video-loaded");
performance.measure("login-duration", "login-started", "login-finished");

const entries = performance.getEntries();

entries.forEach((entry) => {
  if (entry.entryType === "mark") {
    console.log(`${entry.name}'s startTime: ${entry.startTime}`);
  }
  if (entry.entryType === "measure") {
    console.log(`${entry.name}'s duration: ${entry.duration}`);
  }
});

规范

规范
性能时间线
# dom-performance-getentries

浏览器兼容性

另见