性能优化
你一定要经历的 web 页面性能优化之旅 · 02
performance.timing 各种与浏览器性能有关的时间数据,提供浏览器处理网页各个阶段的耗时 performance.timing 对象 performance.timing.navigationStart:当前浏览器窗口的前一个网页关闭,发生 unload 事件时的 Unix 毫秒时间戳。如果没有前一个网页,则等于 fetchStart 属性。 performance.timing.u…
performance.timing 各种与浏览器性能有关的时间数据,提供浏览器处理网页各个阶段的耗时 performance.timing 对象 performance.timing.navigationStart:当前浏览器窗口的前一个网页关闭,发生 unload 事件时的 Unix 毫秒时间戳。如果没有前一个网页,则等于 fetchStart 属性。 performance.timing.u…
各种与浏览器性能有关的时间数据,提供浏览器处理网页各个阶段的耗时
performance.now()
performance.now 方法返回当前网页自从 performance.timing.navigationStart 到当前时间之间的微秒数(毫秒的千分之一)。也就是说,它的精度可以达到 100 万分之一秒
performance.mark() mark 方法用于为相应的视点做标记。
clearMarks 方法用于清除标记,如果不加参数,就表示清除所有标记。
performance.getEntries()
以数组形式,返回这些请求的时间统计信息,有多少个请求,返回数组就会有多少个成员。
performance.getEntriesByType()
函数通过类型获取对应的标记
performance.navigation.type
表示网页的加载来源
performance.navigation.redirectCount
当前网页经过了多少次重定向跳
<body>
<img src="https://pic3.zhimg.com/80/v2-3f18b42f1f7335958072d36641862afa_hd.jpg" />
<script type="text/javascript">
var measure_start = performance.now();
document.getElementsByTagName("img")[0].addEventListener(
"load",
function() {
var measure_end = performance.now();
console.log("image load time: " + (measure_end - measure_start) + "ms");
},
false
);
</script>
</body>
<!-- image load time: 3.8949999725446105ms -->
performance.mark("start");
performance.mark("end");
performance.measure("difference", "start", "end");
var marks = performance.getEntriesByType("mark");
var measures = performance.getEntriesByType("measure");
console.log("===marks===:");
marks.forEach(function(mark) {
console.log(mark.name + ": " + mark.startTime);
});
console.log("===measures===:");
measures.forEach(function(measure) {
console.log(measure.name + ": " + measure.duration);
});
// ===marks===:
// start: 20351.0950000491
// end: 20351.099999970756
// ===measures===:
// difference: 0.004999921657145023
performance.getEntriesByType("resource").forEach(function(r) {
console.log(r.name + ": " + r.duration);
});
// https://pic3.zhimg.com/80/v2-3f18b42f1f7335958072d36641862afa_hd.jpg: 2.740000025369227
performance.clearMarks();
performance.mark("start");
performance.mark("end");
var marks = performance.getEntriesByType("mark");
console.log("before clear:");
marks.forEach(function(mark) {
console.log(mark.name + ": " + mark.startTime);
});
performance.clearMarks("start");
marks = performance.getEntriesByType("mark");
console.log("after clear:");
marks.forEach(function(mark) {
console.log(mark.name + ": " + mark.startTime);
});
// before clear:
// start: 2388638.3650000207
// end: 2388638.4000000544
// after clear:
// end: 2388638.4000000544
Performance API https://www.infoq.cn/article/html5-performance-api-monitoring
使用 HMTL5 API 监控前端性能 https://www.kancloud.cn/dennis/tgjavascript/241845
暂无评论
确认操作
请再次确认编辑图片
待上传图片 (剩余 0 张)
项目详情
确认评论邮箱
邀请制评论