All files performance.js

100% Statements 18/18
100% Branches 2/2
100% Functions 1/1
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 533x 3x 6x 6x 5x     1x 1x       1x       1x       1x         1x           1x       1x       1x     1x     1x     1x   1x    
const util = require('./util');
module.exports = function() {
  const performance = util.getPerformance();
  if (!performance) {
    return {};
  }
 
  const t = performance.timing;
  const times = {};
 
  // 【重要】页面加载完成的时间
  // 【原因】这几乎代表了用户等待页面可用的时间
  times.loadPage = t.loadEventEnd - t.navigationStart;
 
  // 【重要】解析 DOM 树结构的时间
  // 【原因】反省下你的 DOM 树嵌套是不是太多了!
  times.domReady = t.domComplete - t.responseEnd;
 
  // 【重要】重定向的时间
  // 【原因】拒绝重定向!比如,http://example.com/ 就不该写成 http://example.com
  times.redirect = t.redirectEnd - t.redirectStart;
 
  // 【重要】DNS 查询时间
  // 【原因】DNS 预加载做了么?页面内是不是使用了太多不同的域名导致域名查询的时间太长?
  // 可使用 HTML5 Prefetch 预查询 DNS ,见:[HTML5 prefetch](http://segmentfault.com/a/1190000000633364)
  times.lookupDomain = t.domainLookupEnd - t.domainLookupStart;
 
  // 【重要】读取页面第一个字节的时间
  // 【原因】这可以理解为用户拿到你的资源占用的时间,加异地机房了么,加CDN 处理了么?加带宽了么?加 CPU 运算速度了么?
  // TTFB 即 Time To First Byte 的意思
  // 维基百科:https://en.wikipedia.org/wiki/Time_To_First_Byte
  times.ttfb = t.responseStart - t.navigationStart;
 
  // 【重要】内容加载完成的时间
  // 【原因】页面内容经过 gzip 压缩了么,静态资源 css/js 等压缩了么?
  times.request = t.responseEnd - t.requestStart;
 
  // 【重要】执行 onload 回调函数的时间
  // 【原因】是否太多不必要的操作都放到 onload 回调函数里执行了,考虑过延迟加载、按需加载的策略么?
  times.loadEvent = t.loadEventEnd - t.loadEventStart;
 
  // DNS 缓存时间
  times.appcache = t.domainLookupStart - t.fetchStart;
 
  // 浏览器卸载前一个页面(同一个域下)的时间
  times.unloadEvent = t.unloadEventEnd - t.unloadEventStart;
 
  // TCP 建立连接完成握手的时间
  times.connect = t.connectEnd - t.connectStart;
 
  return times;
};