PerformanceResourceTiming: fetchStart 屬性

Baseline 已廣泛支援

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

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

fetchStart 只讀屬性表示在瀏覽器開始獲取資源之前的 timestamp

如果存在 HTTP 重定向,該屬性將返回使用者代理開始獲取重定向鏈中最終資源的之前的時間。

與許多其他 PerformanceResourceTiming 屬性不同,fetchStart 屬性對於跨域請求是可用的,無需 Timing-Allow-Origin HTTP 響應頭。

在瀏覽器開始獲取資源之前的 DOMHighResTimeStamp

示例

測量獲取時間(不含重定向)

fetchStartresponseEnd 屬性可用於測量獲取最終資源(不含重定向)的總時間。如果你想包含重定向,獲取總時間將在 duration 屬性中提供。

js
const timeToFetch = entry.responseEnd - entry.fetchStart;

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

js
const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    const timeToFetch = entry.responseEnd - entry.fetchStart;
    if (timeToFetch > 0) {
      console.log(`${entry.name}: Time to fetch: ${timeToFetch}ms`);
    }
  });
});

observer.observe({ type: "resource", buffered: true });

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

js
const resources = performance.getEntriesByType("resource");
resources.forEach((entry) => {
  const timeToFetch = entry.responseEnd - entry.fetchStart;
  if (timeToFetch > 0) {
    console.log(`${entry.name}: Time to fetch: ${timeToFetch}ms`);
  }
});

規範

規範
資源時序
# dom-performanceresourcetiming-fetchstart

瀏覽器相容性