PerformanceResourceTiming: domainLookupStart 屬性
注意:此功能在 Web Workers 中可用。
domainLookupStart 只讀屬性返回瀏覽器在開始查詢資源的域名名稱之前的 timestamp。
值
domainLookupStart 屬性可以具有以下值
- 在瀏覽器開始查詢資源的域名名稱之前的
DOMHighResTimeStamp。 - 如果資源是從快取中即時檢索的,則為
0。 - 如果資源是跨域請求且未使用
Timing-Allow-OriginHTTP 響應頭,則返回0。
示例
測量 DNS 查詢時間
domainLookupStart 和 domainLookupEnd 屬性可用於測量 DNS 查詢發生所需的時間。
js
const dns = entry.domainLookupEnd - entry.domainLookupStart;
使用 PerformanceObserver 的示例,它會在瀏覽器效能時間線中記錄新的 resource 效能條目時通知。使用 buffered 選項可以訪問觀察者建立之前的條目。
js
const observer = new PerformanceObserver((list) => {
list.getEntries().forEach((entry) => {
const dns = entry.domainLookupEnd - entry.domainLookupStart;
if (dns > 0) {
console.log(`${entry.name}: DNS lookup duration: ${dns}ms`);
}
});
});
observer.observe({ type: "resource", buffered: true });
使用 Performance.getEntriesByType() 的示例,它只顯示在呼叫此方法時瀏覽器效能時間線中存在的 resource 效能條目
js
const resources = performance.getEntriesByType("resource");
resources.forEach((entry) => {
const dns = entry.domainLookupEnd - entry.domainLookupStart;
if (dns > 0) {
console.log(`${entry.name}: DNS lookup duration: ${dns}ms`);
}
});
跨域計時資訊
如果 domainLookupStart 屬性的值為 0,則該資源可能是跨域請求。為了能夠看到跨域時間資訊,需要設定 Timing-Allow-Origin HTTP 響應頭。
例如,要允許 https://mdn.club.tw 檢視時序資源,跨域資源應傳送
http
Timing-Allow-Origin: https://mdn.club.tw
規範
| 規範 |
|---|
| 資源時序 # dom-performanceresourcetiming-domainlookupstart |
瀏覽器相容性
載入中…