统计网页加载时间

Web performance APIs

可以得到各种时间,如dns解析时间,连接时间、跳转时间、加载时间等指标。
这些API需要在页面加载完成后才能使用,需要在window.onload事件中读取各种数据,因为很多值必须在页面完全加载之后才能得出。

  • 页面加载API

 

window.performance.timing 返回一个只读的PerformanceTiming对象,用于分析页面整体性能指标,常用的统计数据如下:

  • DNS查询耗时 :domainLookupEnd - domainLookupStart
  • TCP链接耗时 :connectEnd - connectStart
  • request请求耗时 :responseEnd - responseStart
  • 解析dom树耗时 : domComplete- domInteractive
  • 白屏时间 :responseStart - navigationStart
  • domready时间 :domContentLoadedEventEnd - navigationStart
  • onload时间 :loadEventEnd - navigationStart

其中navigationStart为准备加载新页面的起始时间,loadEventEnd文档触发load事件结束时间

  • 资源加载API

window.performance.getEntries() 显示所有静态资源的加载时间
window.performance.getEntriesByName() 获取相应名字资源的加载时间
window.performance.getEntriesByType() 获取相应类型资源的加载时间

 

posted @ 2020-07-20 11:09  瑞瑞大人  阅读(1162)  评论(0编辑  收藏  举报