PerformanceEntry

Baseline 已广泛支持

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

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

PerformanceEntry 对象封装了浏览器性能时间轴中单个性能指标。

Performance API 提供了内置指标,这些指标是 PerformanceEntry 的专门子类。这包括资源加载、事件计时等的条目。

还可以通过在应用程序的显式点调用 Performance.mark()Performance.measure() 方法来创建性能条目。这允许您将自己的指标添加到性能时间轴中。

PerformanceEntry 实例将始终是以下子类之一

实例属性

PerformanceEntry.name 只读

代表性能条目名称的字符串。值取决于子类型。

PerformanceEntry.entryType 只读

代表性能指标类型的字符串。例如,当使用 PerformanceMark 时为 "mark"

PerformanceEntry.startTime 只读

代表性能指标开始时间的 DOMHighResTimeStamp

PerformanceEntry.duration 只读

代表性能条目持续时间的 DOMHighResTimeStamp

实例方法

PerformanceEntry.toJSON()

返回 PerformanceEntry 对象的 JSON 表示形式。

示例

处理性能条目

以下示例创建了 PerformanceMarkPerformanceMeasure 类型的 PerformanceEntry 对象。PerformanceMarkPerformanceMeasure 子类从 PerformanceEntry 继承 durationentryTypenamestartTime 属性,并将其设置为适当的值。

js
// Place at a location in the code that starts login
performance.mark("login-started");

// Place at a location in the code that finishes login
performance.mark("login-finished");

// Measure login duration
performance.measure("login-duration", "login-started", "login-finished");

function perfObserver(list, observer) {
  list.getEntries().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}`);
    }
  });
}
const observer = new PerformanceObserver(perfObserver);
observer.observe({ entryTypes: ["measure", "mark"] });

规范

规范
性能时间线
# dom-performanceentry

浏览器兼容性