PerformanceEntry: 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() là một serializer; nó trả về biểu diễn JSON của đối tượng PerformanceEntry.

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

Ví dụ

Sử dụng phương thức toJSON

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

js
performance.mark("debug-marker", {
  detail: "debugging-marker-123",
});

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

observer.observe({ entryTypes: ["mark"] });

Kết quả sẽ ghi ra một đối tượng JSON như sau:

json
{
  "name": "debug-marker",
  "entryType": "mark",
  "startTime": 158361,
  "duration": 0
}

Lưu ý rằng nó không chứa thuộc tính detail của PerformanceMark.

Để lấy 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
Performance Timeline
# dom-performanceentry-tojson

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

Xem thêm