PerformanceObserver

Baseline Widely available *

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

* Some parts of this feature may have varying levels of support.

Note: This feature is available in Web Workers.

Giao diện PerformanceObserver được dùng để quan sát các sự kiện đo lường hiệu năng và nhận thông báo về các performance entry mới khi chúng được ghi nhận trong performance timeline của trình duyệt.

Constructor

PerformanceObserver()

Tạo và trả về một đối tượng PerformanceObserver mới.

Thuộc tính tĩnh

PerformanceObserver.supportedEntryTypes Read only

Trả về một mảng các giá trị entryType mà user agent hỗ trợ.

Phương thức instance

PerformanceObserver.observe()

Chỉ định tập hợp các kiểu entry cần quan sát. Hàm callback của performance observer sẽ được gọi khi performance entry được ghi nhận cho một trong các entryTypes đã chỉ định.

PerformanceObserver.disconnect()

Ngừng việc callback của performance observer nhận các performance entry.

PerformanceObserver.takeRecords()

Trả về danh sách hiện tại các performance entry được lưu trong performance observer, đồng thời làm trống danh sách này.

Ví dụ

Tạo một PerformanceObserver

Ví dụ sau tạo một PerformanceObserver để theo dõi các sự kiện "mark" (PerformanceMark) và "measure" (PerformanceMeasure). Hàm callback perfObserver cung cấp một list (PerformanceObserverEntryList) cho phép bạn lấy các performance entry đã được quan sát.

js
function perfObserver(list, observer) {
  list.getEntries().forEach((entry) => {
    if (entry.entryType === "mark") {
      console.log(`${entry.name}'s startTime: ${entry.startTime}`);
    }
    if (entry.entryType === "measure") {
      console.log(`${entry.name}'s duration: ${entry.duration}`);
    }
  });
}
const observer = new PerformanceObserver(perfObserver);
observer.observe({ entryTypes: ["measure", "mark"] });

Thông số kỹ thuật

Thông số kỹ thuật
Performance Timeline
# dom-performanceobserver

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

Xem thêm