PerformanceResourceTiming: domainLookupEnd 属性
注意:此功能在 Web Workers 中可用。
domainLookupEnd 只读属性返回浏览器完成资源域名查找后立即使用的时间戳(timestamp)。
如果用户代理在缓存中具有域名信息,则 domainLookupStart 和 domainLookupEnd 表示用户代理从缓存检索域名数据开始和结束的时间。
值
domainLookupEnd 属性可以具有以下值:
- 一个
DOMHighResTimeStamp,表示浏览器完成资源的域名查找后立即使用的时间。 - 如果资源是从缓存中即时检索的,则为
0。 - 如果资源是跨域请求且未使用
Timing-Allow-OriginHTTP 响应头,则返回0。
示例
测量 DNS 查找时间
domainLookupEnd 和 domainLookupStart 属性可用于测量 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`);
}
});
跨域计时信息
如果 domainLookupEnd 属性的值为 0,则该资源可能是跨域请求。为了能够看到跨域计时信息,需要设置 Timing-Allow-Origin HTTP 响应头。
例如,要允许 https://mdn.org.cn 查看时序资源,跨域资源应发送
http
Timing-Allow-Origin: https://mdn.org.cn
规范
| 规范 |
|---|
| 资源时序 # dom-performanceresourcetiming-domainlookupend |
浏览器兼容性
加载中…