PerformanceNavigationTiming:toJSON() 方法

toJSON() 方法是 PerformanceNavigationTiming 接口的 序列化器;它返回 PerformanceNavigationTiming 对象的 JSON 表示形式。

语法

js
toJSON()

参数

无。

返回值

一个 JSON 对象,它是 PerformanceNavigationTiming 对象的序列化结果。

示例

使用 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()

规范

规范
导航时序级别 2
# dom-performancenavigationtiming-tojson

浏览器兼容性

BCD 表格仅在启用 JavaScript 的浏览器中加载。

另请参见