performance解读页面性能 (chrome)

// 计算程序执行的精确时间
function getFunctionTimeWithDate (func) {  
    var timeStart = Date.now();
 
    // 执行开始
    func();
    // 执行结束
    var timeEnd = Date.now();
 
    // 返回执行时间
    return (timeEnd - timeStart);
}
function getPerformanceTiming () {  
    var performance = window.performance;
 
    if (!performance) {
        // 当前浏览器不支持
        console.log('你的浏览器不支持 performance 接口');
        return;
    }
 
    var t = performance.timing;
    var 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;
}
console.log(getFunctionTimeWithDate(getPerformanceTiming));css

相关文章
相关标签/搜索