PerformanceNavigationTiming: toJSON() 方法
PerformanceNavigationTiming 接口的 toJSON() 方法是一个 序列化器;它会返回 PerformanceNavigationTiming 对象的一个 JSON 表示形式。
语法
js
toJSON()
参数
无。
返回值
一个 PerformanceNavigationTiming 对象的 JSON 表示形式(序列化结果)。
示例
使用 toJSON 方法
在此示例中,调用 entry.toJSON() 将返回 PerformanceNavigationTiming 对象的一个 JSON 表示形式。
js
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
console.log(entry.toJSON());
});
});
observer.observe({ entryTypes: ["navigation"] });
这将记录一个类似如下的 JSON 对象
json
{
"name": "https://en.wikipedia.org/wiki/Main_Page",
"entryType": "navigation",
"startTime": 0,
"duration": 227.60000002384186,
"initiatorType": "navigation",
"nextHopProtocol": "h2",
"renderBlockingStatus": "blocking",
"workerStart": 0,
"redirectStart": 4,
"redirectEnd": 71.40000000596046,
"fetchStart": 71.40000000596046,
"domainLookupStart": 71.40000000596046,
"domainLookupEnd": 71.40000000596046,
"connectStart": 71.40000000596046,
"connectEnd": 71.40000000596046,
"secureConnectionStart": 71.40000000596046,
"requestStart": 73.7000000178814,
"responseStart": 102.90000000596046,
"responseEnd": 105.2000000178814,
"transferSize": 19464,
"encodedBodySize": 19164,
"decodedBodySize": 83352,
"serverTiming": [
{
"name": "cache",
"duration": 0,
"description": "hit-front"
},
{
"name": "host",
"duration": 0,
"description": "cp3062"
}
],
"unloadEventStart": 0,
"unloadEventEnd": 0,
"domInteractive": 178.10000002384186,
"domContentLoadedEventStart": 178.2000000178814,
"domContentLoadedEventEnd": 178.2000000178814,
"domComplete": 227.60000002384186,
"loadEventStart": 227.60000002384186,
"loadEventEnd": 227.60000002384186,
"type": "navigate",
"redirectCount": 1,
"activationStart": 0
}
要获取 JSON 字符串,您可以直接使用 JSON.stringify(entry);它会自动调用 toJSON()。
规范
| 规范 |
|---|
| 导航计时 Level 2 # dom-performancenavigationtiming-tojson |
浏览器兼容性
加载中…