PerformanceResourceTiming: responseEnd 屬性

Baseline 已廣泛支援

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

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

只讀屬性 responseEnd 返回一個 timestamp,該時間戳在瀏覽器接收到資源的最後一個位元組之後立即出現,或者在傳輸連線關閉之前立即出現,以先發生者為準。

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

一個 DOMHighResTimeStamp,該時間戳在瀏覽器接收到資源的最後一個位元組之後立即出現,或者在傳輸連線關閉之前立即出現,以先發生者為準。

示例

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

responseEndfetchStart 屬性可用於衡量獲取最終資源(不含重定向)的總時間。如果您想包含重定向,則獲取總時間在 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-responseend

瀏覽器相容性