PerformanceEntry - 性能时间线的一个指标
PerformanceEntry 对象封装了一个性能指标,该指标是性能时间线的一部分。可以通过在应用程序中的显式位置进行性能 mark 或 measure(例如,通过调用 mark() 方法)来直接创建性能条目。性能条目也可以通过间接方式创建,例如加载资源(例如图像)。
PerformanceEntry 实例将始终是以下子类型之一:
PerformanceMarkPerformanceMeasurePerformanceFrameTimingPerformanceNavigationTimingPerformanceResourceTimingPerformancePaintTiming
注意: 此特性在 Web Worker 中可用。
属性
PerformanceEntry.name 只读
该值进一步指定 PerformanceEntry.entryType 属性返回的值。两者的值取决于子类型。请参阅属性页以获取有效值。
PerformanceEntry.entryType 只读
一个 DOMString,表示性能指标类型(例如 “mark”)的 DOMString}。请参阅属性页以获取有效值。
PerformanceEntry.startTime 只读
一个 DOMHighResTimeStamp,代表性能指标的开始时间。
PerformanceEntry.duration 只读
一个 DOMHighResTimeStamp,代表性能事件持续时间的时间值。
方法
PerformanceEntry.toJSON()
返回 PerformanceEntry 对象的 JSON 表示形式。
实例
以下实例检查所有 PerformanceEntry 属性,以查看浏览器是否支持它们,如果支持,则将其值写入控制台。
function print_PerformanceEntries() {
//使用 getEntries() 获取所有性能条目的列表
var p = performance.getEntries();
for (var i=0; i < p.length; i++) {
console.log("PerformanceEntry[" + i + "]");
print_PerformanceEntry(p[i]);
}
}
function print_PerformanceEntry(perfEntry) {
var properties = ["name",
"entryType",
"startTime",
"duration"];
for (var i=0; i < properties.length; i++) {
// 检查每个属性
var supported = properties[i] in perfEntry;
if (supported) {
var value = perfEntry[properties[i]];
console.log("... " + properties[i] + " = " + value);
} else {
console.log("... " + properties[i] + " 不支持");
}
}
}
规范
| 规范 | 状态 | 备注 |
|---|---|---|
| Resource Timing Level 3 | 编者的草案 | - |
| Resource Timing Level 2 | 工作草案 | - |
| Resource Timing Level 1 | 候选推荐 | 添加 PerformanceResourceTiming 接口和 entryType 的 resource 值。 |
| Navigation Timing Level 2 | 工作草案 | - |
| Navigation Timing | 推荐 | 添加 PerformanceNavigationTiming 接口和 entryType 的 navigation 值。 |
| User Timing Level 2 | 工作草案 | - |
| User Timing | 推荐 | 添加 PerformanceMark 和 PerformanceMeasure 接口以及 entryType 的 mark 和 measure 值。 |
| Frame Timing | 草稿 | 添加 PerformanceFrameTiming 接口和 entryType 的 frame 值。 |
| Performance Timeline Level 2 PerformanceEntry 的定义 |
候选推荐 | 添加了 toJSON() 序列化方法。 |
| Performance Timeline PerformanceEntry 的定义 |
推荐 | 初始定义。 |
桌面浏览器兼容性
| 特性 | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| 基础支持 | 46 25 webkit | 支持 | 支持 | 支持 | 33 | 11 |
duration | 支持 | 12 | 支持 | 支持 | 支持 | 11 |
entryType | 支持 | 12 | 支持 | 支持 | 支持 | 11 |
name | 支持 | 12 | 支持 | 支持 | 支持 | 11 |
startTime | 支持 | 12 | 支持 | 支持 | 支持 | 11 |
toJSON | 支持 | 16 | 支持 | 不支持 | 支持 | 11 |
| 在 Worker 中可用 | 62 | 未知 | 60 | 未知 | 49 | 未知 |