Performance: 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 April 2021.

Note: This feature is available in Web Workers.

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

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 Performance.

JSON được trả về không chứa thuộc tính eventCounts vì nó có kiểu EventCounts, và kiểu này không cung cấp thao tác toJSON().

Note: Đối tượng JSON chứa kết quả tuần tự hóa của các thuộc tính đã lỗi thời performance.timingperformance.navigation. Để lấy biểu diễn JSON của giao diện mới hơn PerformanceNavigationTiming, hãy gọi PerformanceNavigationTiming.toJSON() thay thế.

Ví dụ

Dùng phương thức toJSON

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

js
performance.toJSON();

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

json
{
  "timeOrigin": 1668077531367.4,
  "timing": {
    "connectStart": 1668077531372,
    "navigationStart": 1668077531367,
    "secureConnectionStart": 0,
    "fetchStart": 1668077531372,
    "domContentLoadedEventStart": 1668077531580,
    "responseStart": 1668077531372,
    "domInteractive": 1668077531524,
    "domainLookupEnd": 1668077531372,
    "responseEnd": 1668077531500,
    "redirectStart": 0,
    "requestStart": 1668077531372,
    "unloadEventEnd": 0,
    "unloadEventStart": 0,
    "domLoading": 1668077531512,
    "domComplete": 1668077531585,
    "domainLookupStart": 1668077531372,
    "loadEventStart": 1668077531585,
    "domContentLoadedEventEnd": 1668077531580,
    "loadEventEnd": 1668077531585,
    "redirectEnd": 0,
    "connectEnd": 1668077531372
  },
  "navigation": {
    "type": 0,
    "redirectCount": 0
  }
}

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

Thông số kỹ thuật

Specification
High Resolution Time
# dom-performance-tojson

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

Xem thêm