PerformanceResourceTiming: toJSON() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2017.

Note: This feature is available in Web Workers.

Phương thức toJSON() của giao diện PerformanceResourceTiming là một serializer; nó trả về biểu diễn JSON của đối tượng PerformanceResourceTiming.

Cú pháp

js
toJSON()

Tham số

Không có.

Giá trị trả về

Một đối tượng JSON là kết quả tuần tự hóa của đối tượng PerformanceResourceTiming.

Ví dụ

Dùng phương thức toJSON

Trong ví dụ này, gọi entry.toJSON() sẽ trả về biểu diễn JSON của đối tượng PerformanceResourceTiming.

js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    console.log(entry.toJSON());
  });
});

observer.observe({ type: "resource", buffered: true });

Điều này sẽ ghi ra một đối tượng JSON như sau:

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"
    }
  ]
}

Để lấy một chuỗi JSON, bạn có thể dùng trực tiếp JSON.stringify(entry); nó sẽ tự động gọi toJSON().

Thông số kỹ thuật

Specification
Resource Timing
# dom-performanceresourcetiming-tojson

Tương thích trình duyệt

Xem thêm