PerformanceEntry
注意:此功能在 Web Workers 中可用。
PerformanceEntry 对象封装了浏览器性能时间轴中单个性能指标。
Performance API 提供了内置指标,这些指标是 PerformanceEntry 的专门子类。这包括资源加载、事件计时等的条目。
还可以通过在应用程序的显式点调用 Performance.mark() 或 Performance.measure() 方法来创建性能条目。这允许您将自己的指标添加到性能时间轴中。
PerformanceEntry 实例将始终是以下子类之一
LargestContentfulPaintLayoutShiftPerformanceEventTimingPerformanceLongAnimationFrameTimingPerformanceLongTaskTimingPerformanceMarkPerformanceMeasurePerformanceNavigationTimingPerformancePaintTimingPerformanceResourceTimingPerformanceScriptTimingPerformanceServerTimingTaskAttributionTimingVisibilityStateEntry
实例属性
PerformanceEntry.name只读-
代表性能条目名称的字符串。值取决于子类型。
PerformanceEntry.entryType只读-
代表性能指标类型的字符串。例如,当使用
PerformanceMark时为"mark"。 PerformanceEntry.startTime只读-
代表性能指标开始时间的
DOMHighResTimeStamp。 PerformanceEntry.duration只读-
代表性能条目持续时间的
DOMHighResTimeStamp。
实例方法
PerformanceEntry.toJSON()-
返回
PerformanceEntry对象的 JSON 表示形式。
示例
处理性能条目
以下示例创建了 PerformanceMark 和 PerformanceMeasure 类型的 PerformanceEntry 对象。PerformanceMark 和 PerformanceMeasure 子类从 PerformanceEntry 继承 duration、entryType、name 和 startTime 属性,并将其设置为适当的值。
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 |
浏览器兼容性
加载中…