常用公式和介绍

前提:在页面load时候也就是加载完再去执行这个api,eg:

  1. window.addEventListener('load', (event) => {
  2. const timing = performance.getEntriesByType('navigation')[0];
  3. const tti = timing.domInteractive - timing.fetchStart; // 首次可交互时间
  4. })

DNS 解析耗时

  • 公式:domainLookupEnd - domainLookupStart

    TCP 连接耗时

  • 公式: connectEnd - connectStart

    SSL 安全连接耗时:

  • 公式:connectEnd - secureConnectionStart

    网络请求耗时 (TTFB)

  • 公式:responseStart - requestStart

    数据传输耗时

  • 公式:responseEnd - responseStart

    DOM 解析耗时

  • 公式:domInteractive - responseEnd

    资源加载耗时

  • 公式:loadEventStart - domContentLoadedEventEnd

    First Byte时间

  • 公式:responseStart - domainLookupStart

    白屏时间

  • 公式:responseEnd - fetchStart

    首次可交互时间

  • 公式:domInteractive - fetchStart

    DOM Ready 时间

  • 公式:domContentLoadEventEnd - fetchStart

    页面完全加载时间

  • 公式:loadEventStart - fetchStart

    http 头部大小

  • 公式:transferSize - encodedBodySize

    重定向次数

  • performance.navigation.redirectCount

    重定向耗时

  • 公式:redirectEnd - redirectStart

    api说明:

  • domainLookupEnd:解析域名结束时的Unix毫秒时间戳

  • domainLookupStart:域名开始解析之时的Unix毫秒时间戳
  • connectEnd:网络链接建立的时间节点
  • connectStart:请求连接被发送到网络之时的Unix毫秒时间戳
  • secureConnectionStart:安全连接握手开始的时刻的 Unix毫秒时间戳
  • responseStart:浏览器从服务器、缓存或者本地资源接收到响应的第一个字节之时的 Unix毫秒时间戳
  • requestStart:浏览器发送从服务器或者缓存获取实际文档的请求之时的 Unix毫秒时间戳
  • domInteractive:主文档的解析器结束工作
  • responseEnd:浏览器从服务器、缓存或者本地资源接收响应的最后一个字节或者连接被关闭之时的 Unix毫秒时间戳
  • loadEventStart:事件被现在的文档触发之时的 Unix时间戳
  • domContentLoadedEventEnd:即DOM Ready
  • fetchStart:为浏览器已经准备好去使用HTTP请求抓取文档之时的 Unix毫秒时间戳
  • domainLookupStart:域名开始解析之时的 Unix毫秒时间戳
  • redirectStart:第一个HTTP的重定向开始的时刻的 Unix毫秒时间戳
  • redirectEnd:最后一次的HTTP重定向被完成且HTTP响应的最后一个字节被接收之时的 Unix毫秒时间戳

获取页面离开进入api:visibilitychange

  • 监听事件:visibilitychange 兼容:webvisibilitychange
  • 主要api:document.hidden 兼容:document.webkitHidden

    1. let vEvent = 'visibilitychange';
    2. if (document.webkitHidden !== undefined) { // 这里做一下兼容
    3. vEvent = 'webkitvisibilitychange';
    4. }
    5. function visibleChanged() {
    6. if (document.hidden || document.webkitHidden) {
    7. console.log('web page is hidden');
    8. } else {
    9. console.log('web page is visible')
    10. }
    11. }
    12. document.addEventListener(vEvent, visibleChanged, false)

    获取判断网络好坏api:

  • navigator.connection.effectiveType

    1. let connnection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
    2. let type = connection.effectiveType;
    3. function updateConnectionStatus() {
    4. console.log('connection type change from ' + type + 'to' + connection.effectiveType);
    5. }
    6. connection.addEventListener('change', updateConnectionStatus)