Performance: getEntries() method
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2017.
Note: This feature is available in Web Workers.
Phương thức getEntries() trả về một mảng chứa tất cả các đối tượng PerformanceEntry hiện có trong performance timeline.
Nếu bạn chỉ quan tâm đến performance entry có kiểu hoặc tên nhất định, xem getEntriesByType() và getEntriesByName().
Note:
Phương thức này không thông báo cho bạn về performance entry mới; bạn chỉ nhận được những entry đang có trong performance timeline tại thời điểm gọi phương thức.
Để nhận thông báo khi entry mới xuất hiện, hãy dùng PerformanceObserver.
Các kiểu entry sau không được phương thức này hỗ trợ và sẽ không bao giờ được trả về, ngay cả khi entry cho chúng tồn tại:
"element"(PerformanceElementTiming)"event"(PerformanceEventTiming)"largest-contentful-paint"(LargestContentfulPaint)"layout-shift"(LayoutShift)"longtask"(PerformanceLongTaskTiming)
Để truy cập các entry này, bạn phải dùng PerformanceObserver.
Cú pháp
getEntries()
Tham số
Không có.
Giá trị trả về
Một Array các đối tượng PerformanceEntry. Các phần tử sẽ ở thứ tự thời gian dựa trên startTime của từng entry.
Ví dụ
>Ghi nhật ký tất cả marker và measure hiệu năng
Giả sử bạn đã tạo các đối tượng PerformanceMark và PerformanceMeasure ở những vị trí phù hợp trong mã của mình, bạn có thể muốn ghi tất cả chúng ra console như sau:
// Ví dụ marker/measure
performance.mark("login-started");
performance.mark("login-finished");
performance.mark("form-sent");
performance.mark("video-loaded");
performance.measure("login-duration", "login-started", "login-finished");
const entries = performance.getEntries();
entries.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}`);
}
});
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| Performance Timeline> # dom-performance-getentries> |