PerformanceResourceTiming: toJSON() 方法
注意:此功能在 Web Workers 中可用。
toJSON() 方法是 PerformanceResourceTiming 接口的一个 序列化器;它返回 PerformanceResourceTiming 对象的 JSON 表示形式。
语法
js
toJSON()
参数
无。
返回值
一个 JSON 对象,表示 PerformanceResourceTiming 对象的序列化结果。
示例
使用 toJSON 方法
在此示例中,调用 entry.toJSON() 会返回 PerformanceResourceTiming 对象的 JSON 表示形式。
js
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
console.log(entry.toJSON());
});
});
observer.observe({ type: "resource", buffered: true });
这将记录一个类似如下的 JSON 对象
json
{
"name": "https://upload.wikimedia.org/wikipedia/en/thumb/4/4a/Commons-logo.svg/31px-Commons-logo.svg.png",
"entryType": "resource",
"startTime": 110.80000001192093,
"duration": 11.599999994039536,
"initiatorType": "img",
"nextHopProtocol": "h2",
"renderBlockingStatus": "non-blocking",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 110.80000001192093,
"domainLookupStart": 110.80000001192093,
"domainLookupEnd": 110.80000001192093,
"connectStart": 110.80000001192093,
"connectEnd": 110.80000001192093,
"secureConnectionStart": 110.80000001192093,
"requestStart": 117.30000001192093,
"responseStart": 120.40000000596046,
"responseStatus": 200,
"responseEnd": 122.40000000596046,
"transferSize": 0,
"encodedBodySize": 880,
"decodedBodySize": 880,
"serverTiming": [
{
"name": "cache",
"duration": 0,
"description": "hit-front"
},
{
"name": "host",
"duration": 0,
"description": "cp3061"
}
]
}
要获取 JSON 字符串,您可以直接使用 JSON.stringify(entry);它会自动调用 toJSON()。
规范
| 规范 |
|---|
| 资源时序 # dom-performanceresourcetiming-tojson |
浏览器兼容性
加载中…