[Vue 2] 新版本探究之 performance 相关

作者:滴滴公共前端 小春

前言:

最近各大前端技术周刊或者关注使用 Vue 的同学都在讨论前几天又出新版了 v2.2.1

在此我们调研一下,各位同学现在用 Vue 2.* 一般是哪个版本呢?可以给我们留言哦~

正文:

对于任何框架,我自己都喜欢看 releases,也比较关注版本和版本直接的差异。本文我们讨论一点和 performance 有关的东西。
2.2.0 的版本开始,我们发现 Vue 的源码里面多了一个东东:

// config 里面多了一个配置
var config = {
  performance: "development" !== 'production'
}复制代码

官方 releases 的说明:

New config: Vue.config.performance. Setting it to true traces component init, compile, render and patch time in the browser devtool timeline.

Only available in dev mode.

我们看看源码里面:

// 内部赋值 perf,后面会用到这个变量
// 核心还是:window.performance
var perf;
{
  perf = inBrowser && window.performance;
  if (perf && (!perf.mark || !perf.measure)) {
    perf = undefined;
  }
}复制代码

如何整体地看 Vue 一共依赖 perf 打了多少个点:

window.performance.getEntries()

截图如下:

不熟悉 performance 的 getEntries 的同学可以点击:Performance.getEntries()

简单归纳:

列出通过 mark 和 measure 打点的列表
当然也可以传递参数

我们具体看看代码里面有哪些地方用到了 perf

Vue.prototype._init = function (options) {
  if ("development" !== 'production' && config.performance && perf) {
    // 这里是第一个
    perf.mark('init');
  }
}复制代码
if ("development" !== 'production' && config.performance && perf) {
    vm._name = formatComponentName(vm, false);
    //mark 了一个 init end
    perf.mark('init end');
    //measure 了一下
    perf.measure(((vm._name) + " init"), 'init', 'init end');
}复制代码

关于 mark 不熟悉的请点击:Performance.mark()

// 可以标记一些指定 name 对应的时间点
creates a timestamp in the browser's performance entry buffer with the given name.
// duration 一般都是 0

语法:

performance.mark(name)

也可以通过下面方式来查看所有 mark 过的数据:

window.performance.getEntriesByType("mark")

截图如下:

那 measure 呢,更多可以点击:Performance.measure()

creates a named timestamp in the browser's performance entry buffer between two specified marks (known as the start mark and end mark, respectively).

The named timestamp is referred to as a measure

查看:

window.performance.getEntriesByType("measure")

截图如下:


欢迎关注DDFE
GITHUB:github.com/DDFE
微信公众号:微信搜索公众号“DDFE”或扫描下面的二维码


本文对你有帮助?欢迎扫码加入前端学习小组微信群:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值