work让前端飞Web前端之路

Performance — 带你监控前端性能

2018-04-12  本文已影响150人  Xiao呆不糊涂

好久没有写内容了...最近真的是疏于学习...按照惯例先安利下自己的Git....如果有错误或者理解不正确的地方,麻烦告知我会及时更正。同时也非常欢迎大家一起讨论。鞠躬。 Github地址:https://github.com/bsxz0604/RemarkForYouDontKnowJs麻烦各位兄弟姐妹点个star...(ps:祝Github十岁快乐


这个是我根据Performance 做的一个小的插件...感兴趣的可以试用一下
https://github.com/bsxz0604/FE_Performance_Timing


我们先看到的是navigation timing监测指标图,是HTML5提供的一组新API,已经在很多主流的浏览器中实现了,最后会有图说明支持的浏览器(不想承认IE是主流浏览器ing….


RequestToDoWhat.jpg navigationTime.png

可以看到浏览器处理请求会经历了下面这些阶段:重定向→拉取缓存→DNS查询→建立TCP链接→发起请求→接收响应→处理HTML元素→元素加载完成,而这些时间数据都会Performance对象中体现出来

按触发顺序排列所有属性,详细的可以查看Navigation Timing

属性

window.performance //返回一个Performance对象

interface Performance {
  readonly attribute PerformanceTiming timing;
  readonly attribute PerformanceNavigation navigation;
};

interface PerformanceTiming {
  readonly attribute unsigned long long navigationStart;
  readonly attribute unsigned long long unloadEventStart;
  readonly attribute unsigned long long unloadEventEnd;
  readonly attribute unsigned long long redirectStart;
  readonly attribute unsigned long long redirectEnd;
  readonly attribute unsigned long long fetchStart;
  readonly attribute unsigned long long domainLookupStart;
  readonly attribute unsigned long long domainLookupEnd;
  readonly attribute unsigned long long connectStart;
  readonly attribute unsigned long long connectEnd;
  readonly attribute unsigned long long secureConnectionStart;
  readonly attribute unsigned long long requestStart;
  readonly attribute unsigned long long responseStart;
  readonly attribute unsigned long long responseEnd;
  readonly attribute unsigned long long domLoading;
  readonly attribute unsigned long long domInteractive;
  readonly attribute unsigned long long domContentLoadedEventStart;
  readonly attribute unsigned long long domContentLoadedEventEnd;
  readonly attribute unsigned long long domComplete;
  readonly attribute unsigned long long loadEventStart;
  readonly attribute unsigned long long loadEventEnd;
};

interface PerformanceNavigation {
  const unsigned short TYPE_NAVIGATE = 0; //普通进入,包括:点击链接、在地址栏中输入 URL、表单提交、或者通过除下表中 TYPE_RELOAD 和 TYPE_BACK_FORWARD 的方式初始化脚本
  const unsigned short TYPE_RELOAD = 1; // 通过刷新进入,包括:浏览器的刷新按钮、快捷键刷新、location.reload()等方法。
  const unsigned short TYPE_BACK_FORWARD = 2; //通过操作历史记录进入,包括:浏览器的前进后退按钮、快捷键操作、history.forward()、history.back()、history.go(num)
  const unsigned short TYPE_RESERVED = 255; // 其他
  readonly attribute unsigned short type;
  readonly attribute unsigned short redirectCount; //表示到达最终页面前,重定向的次数,但是这个接口有同源策略限制,即仅能检测同源的重定向。
}

由于这些内容都差不多是字面意思.而且W3C上解释的很清楚,所以就不展开了(其实就是我懒ORZ…当然.为了不浪费大家时间这个….

列出PerformanceTiming中一些常用的计算:

PerformanceNavigation中的属性查看上面接口代码后的注释,暂时在工作中没有使用到
ps: 如果你使用的是chrome, MemoryInfo是一个非标准属性
* jsHeapSizeLimit: 内存大小限制
* totalJSHeapSize: 可使用的内存
* usedJSHeapSize: JS对象(包括V8引擎内部对象)占用的内存,不能大于totalJSHeapSize,如果大于,有可能出现了内存泄漏

方法

总结

Performance用来做前端性能监控,就2个字形容:利器...
当然这只是前端性能优化的第一步(性能的路上我也只看到了冰山一角),道阻且长 ~ 所以希望大家提出问题和指出疑问,一起进步~

之前写的文章大家感兴趣的话,也可以阅读了解讨论:
HTTP协议浅谈
Javascript作用域和闭包

上一篇 下一篇

猜你喜欢

热点阅读