Performance: getEntries() 方法

Baseline 已廣泛支援

此功能已成熟,可跨多種裝置和瀏覽器版本使用。自 2017 年 9 月以來,它已在瀏覽器中提供。

注意:此功能在 Web Workers 中可用。

getEntries() 方法返回一個包含當前效能時間線中所有 PerformanceEntry 物件的陣列。

如果您只對特定型別或特定名稱的效能條目感興趣,請參閱 getEntriesByType()getEntriesByName()

注意:此方法不會通知您新的效能條目;您只會獲得在呼叫此方法時效能時間軸中存在的條目。要接收有關可用條目的通知,請使用 PerformanceObserver

以下條目型別完全不受此方法支援,即使可能存在這些型別的條目也不會返回

要訪問這些型別的條目,您必須改用 PerformanceObserver

語法

js
getEntries()

引數

無。

返回值

一個包含 PerformanceEntry 物件的 Array。這些條目將按照其 startTime 的時間順序排列。

示例

記錄所有效能標記和度量

假設您在程式碼的適當位置建立了自己的 PerformanceMarkPerformanceMeasure 物件,您可能會像這樣將它們全部記錄到控制檯:

js
// Example markers/measures
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}`);
  }
});

規範

規範
效能時間線
# dom-performance-getentries

瀏覽器相容性

另見