PerformanceServerTiming: duration 屬性

Baseline 已廣泛支援

此功能已成熟,並可在許多裝置和瀏覽器版本上執行。自 2023 年 3 月以來,它已在各種瀏覽器中可用。

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

duration 只讀屬性返回一個雙精度浮點數,包含伺服器指定的指標持續時間,或者返回 0.0

一個數字。

示例

記錄伺服器計時條目

伺服器計時指標需要伺服器傳送 Server-Timing 標頭。例如:

http
Server-Timing: cache;desc="Cache Read";dur=23.2

serverTiming 條目可以存在於 navigationresource 條目上。

使用 PerformanceObserver 的示例,它會在 navigationresource 效能條目被記錄到瀏覽器效能時間線上時通知。使用 buffered 選項可以訪問觀察者建立之前的條目。

js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    entry.serverTiming.forEach((serverEntry) => {
      console.log(
        `${serverEntry.name} (${serverEntry.description}) duration: ${serverEntry.duration}`,
      );
      // Logs "cache (Cache Read) duration: 23.2"
    });
  });
});

["navigation", "resource"].forEach((type) =>
  observer.observe({ type, buffered: true }),
);

使用 Performance.getEntriesByType() 的示例,它僅顯示在呼叫此方法時存在於瀏覽器效能時間線中的 navigationresource 效能條目。

js
for (const entryType of ["navigation", "resource"]) {
  for (const { name: url, serverTiming } of performance.getEntriesByType(
    entryType,
  )) {
    if (serverTiming) {
      for (const { name, description, duration } of serverTiming) {
        console.log(`${name} (${description}) duration: ${duration}`);
        // Logs "cache (Cache Read) duration: 23.2"
      }
    }
  }
}

規範

規範
伺服器計時
# dom-performanceservertiming-duration

瀏覽器相容性

另見