window.performance

705 阅读3分钟

image.png

window.performance.timing

image.png

image.png

按触发顺序排列所有属性:(更详细标准的解释请参看:W3C Editor's Draft)

navigationStart:在同一个浏览器上下文中,前一个网页(与当前页面不一定同域)unload 的时间戳,如果无前一个网页 unload ,则与 fetchStart 值相等

unloadEventStart:前一个网页(与当前页面同域)unload 的时间戳,如果无前一个网页 unload 或者前一个网页与当前页面不同域,则值为 0

unloadEventEnd:和 unloadEventStart 相对应,返回前一个网页 unload 事件绑定的回调函数执行完毕的时间戳

redirectStart:第一个 HTTP 重定向发生时的时间。有跳转且是同域名内的重定向才算,否则值为 0

redirectEnd:最后一个 HTTP 重定向完成时的时间。有跳转且是同域名内的重定向才算,否则值为 0

fetchStart:浏览器准备好使用 HTTP 请求抓取文档的时间,这发生在检查本地缓存之前

domainLookupStart:DNS 域名查询开始的时间,如果使用了本地缓存(即无 DNS 查询)或持久连接,则与 fetchStart 值相等

domainLookupEnd:DNS 域名查询完成的时间,如果使用了本地缓存(即无 DNS 查询)或持久连接,则与 fetchStart 值相等

connectStart:HTTP(TCP) 开始建立连接的时间,如果是持久连接,则与 fetchStart 值相等,如果在传输层发生了错误且重新建立连接,则这里显示的是新建立的连接开始的时间

connectEnd:HTTP(TCP) 完成建立连接的时间(完成握手),如果是持久连接,则与 fetchStart 值相等,如果在传输层发生了错误且重新建立连接,则这里显示的是新建立的连接完成的时间

注意: 这里握手结束,包括安全连接建立完成、SOCKS 授权通过

secureConnectionStart:HTTPS 连接开始的时间,如果不是安全连接,则值为 0

requestStart:HTTP 请求读取真实文档开始的时间(完成建立连接),包括从本地读取缓存,连接错误重连时,这里显示的也是新建立连接的时间

responseStart:HTTP 开始接收响应的时间(获取到第一个字节),包括从本地读取缓存

responseEnd:HTTP 响应全部接收完成的时间(获取到最后一个字节),包括从本地读取缓存

domLoading:开始解析渲染 DOM 树的时间,此时 Document.readyState 变为 loading,并将抛出 readystatechange 相关事件

domInteractive:完成解析 DOM 树的时间,Document.readyState 变为 interactive,并将抛出 readystatechange 相关事件

注意: 只是 DOM 树解析完成,这时候并没有开始加载网页内的资源

domContentLoadedEventStart:DOM 解析完成后,网页内资源加载开始的时间,文档发生 DOMContentLoaded事件的时间

domContentLoadedEventEnd:DOM 解析完成后,网页内资源加载完成的时间(如 JS 脚本加载执行完毕),文档的DOMContentLoaded 事件的结束时间

domComplete:DOM 树解析完成,且资源也准备就绪的时间,Document.readyState 变为 complete,并将抛出 readystatechange 相关事件

loadEventStart:load 事件发送给文档,也即 load 回调函数开始执行的时间,如果没有绑定 load 事件,值为 0

loadEventEnd:load 事件的回调函数执行完毕的时间,如果没有绑定 load 事件,值为 0

常用计算:

DNS查询耗时 :domainLookupEnd - domainLookupStart

TCP链接耗时 :connectEnd - connectStart

request请求耗时 :responseEnd - responseStart

解析dom树耗时 : domComplete - domInteractive

白屏时间 :responseStart - navigationStart

domready时间(用户可操作时间节点) :domContentLoadedEventEnd - navigationStart

onload时间(总下载时间) :loadEventEnd - navigationStart

window.performance.getEntries()

获取所有资源请求的时间数据。

  • entryType:资源类型
  • initiatorType: 谁发起的请求
entryTypeinitiatorType
navigationnavigation
sourcelink/script/img/iframe等
mark...
measure...
paintfirst-paint(首次绘制)/first-contentful-paint(首次内容绘制)
  • mark和measure的应用:
window.performance.mark('time-start')

fetch('/b?id=2').then(res => {
    window.performance.mark('time-end')
    window.performance.measure('record-time', 'time-start', 'time-end')
    console.log('time-start', window.performance.getEntriesByName('time-start'))
    console.log('time-end', window.performance.getEntriesByName('time-end'))
    console.log('record-time', window.performance.getEntriesByName('record-time'))
})

image.png

  • source之link的应用:
 <link rel="stylesheet" href="https://g.alicdn.com/kg/??search-suggest/6.3.1/new_suggest-min.css">

image.png

  • source之fetch的应用:
fetch('/a?id=1').then((res)=>{
   
})

fetch('/b?id=2').then(res => {
   
})

image.png

image.png

window.performance.getEntriesByName()

window.performance.getEntriesByType()

image.png