改善頁面性能 - 開發者需要了解的 Performance API 標準
背景
為了提供給前端者監控頁面性能的能力,W3C 定義了一系列相關 API,這裡統稱為 Performance API。目前使用應用廣泛的是 PerformanceTiming,但是除了該 API,新的 W3C 草案及 WICG 提案定義了一系列 PerformanceEntry API。新 API 不僅取代了原 PerformanceTiming 的能力,並增加了更多維度的信息。本文主要針對這些 API 進行介紹。
為了更好地理解目前這些 API 的成熟度,先簡單介紹一下 W3C 標準發布流程:
在 W3C 發布一個新標準的過程中,規範是通過下面的嚴格程序由一個簡單的理念逐步確立為推薦標準的:
- W3C 收到一份提交
- 由 W3C 發布一份記錄
- 由 W3C 創建一個工作組
- 由 W3C 發布一份工作草案
- 由 W3C 發布一份候選的推薦
- 由 W3C 發布一份被提議的推薦
- 由 W3C 發布推薦
文檔演進過程:
W3C 提交 (W3C Submissions) --> W3C 記錄 (W3C Notes) --> W3C 工作組 (W3C Working Groups) --> W3C 工作草案 (W3C Working Drafts) --> W3C 候選推薦 (W3C Candidate Recommendations) --> W3C 提議推薦 (W3C Proposed Recommendations) --> W3C 推薦 (W3C Recommendations)
PerformanceTiming
早期的 Performance 相關信息主要是由 Navigation Timing Recommendations 定義的 PerformanceTiming 提供,包含了載入過程的細分信息:
interfacePerformanceTiming {
readonly attribute unsigned long long navigationStart;
readonly attribute unsigned long long unloadEventStart;
readonly attribute unsigned long long unloadEventEnd;
readonly attribute unsigned long long redirectStart;
readonly attribute unsigned long long redirectEnd;
readonly attribute unsigned long long fetchStart;
readonly attribute unsigned long long domainLookupStart;
readonly attribute unsigned long long domainLookupEnd;
readonly attribute unsigned long long connectStart;
readonly attribute unsigned long long connectEnd;
readonly attribute unsigned long long secureConnectionStart;
readonly attribute unsigned long long requestStart;
readonly attribute unsigned long long responseStart;
readonly attribute unsigned long long responseEnd;
readonly attribute unsigned long long domLoading;
readonly attribute unsigned long long domInteractive;
readonly attribute unsigned long long domContentLoadedEventStart;
readonly attribute unsigned long long domContentLoadedEventEnd;
readonly attribute unsigned long long domComplete;
readonly attribute unsigned long long loadEventStart;
readonly attribute unsigned long long loadEventEnd;};
每個屬性的含義用下面的圖可以直觀地描述: