当前位置: 首页 > news >正文

为何Vue3比Vue2快

Proxy响应式

PatchFlag

  • 编译模板时,动态节点做标记
  • 标记,分为不同的类型,如TEXT PROPS
  • diff算法时,可以区分静态节点,以及不同类型的动态节点
<div>Hello World</div>
<span>{{ msg }}</span>
<span class="msg">闻人放歌</span>
<span id="name">{{ msg }}</span>
<span class="msg-class" :msg="message">{{ msg }}</span>

编译结果:

import { createElementVNode as _createElementVNode, toDisplayString as _toDisplayString, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"export function render(_ctx, _cache, $props, $setup, $data, $options) {return (_openBlock(), _createElementBlock(_Fragment, null, [_createElementVNode("div", null, "Hello World"),_createElementVNode("span", null, _toDisplayString(_ctx.msg), 1 /* TEXT */),_createElementVNode("span", { class: "msg" }, "闻人放歌"),_createElementVNode("span", { id: "name" }, _toDisplayString(_ctx.msg), 1 /* TEXT */),_createElementVNode("span", {class: "msg-class",msg: _ctx.message}, _toDisplayString(_ctx.msg), 9 /* TEXT, PROPS */, ["msg"])], 64 /* STABLE_FRAGMENT */))
}// Check the console for the AST

在这里插入图片描述
静态字段将不再比较,这样diff算法将少比较很多节点。

hoistStatic

  • 将静态节点的定义,提升到父作用域,缓存起来
  • 多个相邻的静态节点,会被合并起来
  • 典型的拿空间换时间的优化策略
//  模板
<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>

编译结果:

import { createElementVNode as _createElementVNode, createCommentVNode as _createCommentVNode, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"const _hoisted_1 = /*#__PURE__*/_createElementVNode("div", null, "Hello World", -1 /* HOISTED */)
const _hoisted_2 = /*#__PURE__*/_createElementVNode("div", null, "Hello World", -1 /* HOISTED */)
const _hoisted_3 = /*#__PURE__*/_createElementVNode("div", null, "Hello World", -1 /* HOISTED */)export function render(_ctx, _cache, $props, $setup, $data, $options) {return (_openBlock(), _createElementBlock(_Fragment, null, [_hoisted_1,_hoisted_2,_hoisted_3,_createCommentVNode(" <span>{{ msg }}</span>\n<span class=\"msg\">闻人放歌</span>\n<span id=\"name\">{{ msg }}</span>\n<span class=\"msg-class\" :msg=\"message\">{{ msg }}</span> ")], 64 /* STABLE_FRAGMENT */))
}// Check the console for the AST

还有一个点,如果静态节点数量多,编译会自动合并,如:

<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
import { createElementVNode as _createElementVNode, createCommentVNode as _createCommentVNode, createStaticVNode as _createStaticVNode, Fragment as _Fragment, openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"const _hoisted_1 = /*#__PURE__*/_createStaticVNode("<div>Hello World</div><div>Hello World</div><div>Hello World</div><div>Hello World</div><div>Hello World</div><div>Hello World</div><div>Hello World</div><div>Hello World</div><div>Hello World</div><div>Hello World</div>", 10)export function render(_ctx, _cache, $props, $setup, $data, $options) {return (_openBlock(), _createElementBlock(_Fragment, null, [_hoisted_1,_createCommentVNode(" <span>{{ msg }}</span>\n<span class=\"msg\">闻人放歌</span>\n<span id=\"name\">{{ msg }}</span>\n<span class=\"msg-class\" :msg=\"message\">{{ msg }}</span> ")], 2112 /* STABLE_FRAGMENT, DEV_ROOT_FRAGMENT */))
}// Check the console for the AST

cacheHandler

上代码:

<div @click="clickHandler">Hello World</div>

编译结果:

import { openBlock as _openBlock, createElementBlock as _createElementBlock } from "vue"export function render(_ctx, _cache, $props, $setup, $data, $options) {return (_openBlock(), _createElementBlock("div", {onClick: _cache[0] || (_cache[0] = (...args) => (_ctx.clickHandler && _ctx.clickHandler(...args)))}, "Hello World"))
}// Check the console for the AST

从编译结果代码可以看出,如果该事件方法已被定义则直接去缓存中的方法,没有则定义。其意义就是缓存事件

SSR优化

编译前:

<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
<div>Hello World</div>
<div>{{ msg }}</div>
<div @click="clickHandler">Hello World</div>
<div class="msg-class">{{ msg }}</div>
<div class="msg-class" :msg="msg">闻人放歌</div>

编译结果:

import { mergeProps as _mergeProps } from "vue"
import { ssrRenderAttrs as _ssrRenderAttrs, ssrInterpolate as _ssrInterpolate } from "vue/server-renderer"export function ssrRender(_ctx, _push, _parent, _attrs, $props, $setup, $data, $options) {const _cssVars = { style: { color: _ctx.color }}_push(`<!--[--><div${_ssrRenderAttrs(_cssVars)}>Hello World</div><div${_ssrRenderAttrs(_cssVars)}>Hello World</div><div${_ssrRenderAttrs(_cssVars)}>Hello World</div><div${_ssrRenderAttrs(_cssVars)}>Hello World</div><div${_ssrRenderAttrs(_cssVars)}>Hello World</div><div${_ssrRenderAttrs(_cssVars)}>Hello World</div><div${_ssrRenderAttrs(_cssVars)}>${_ssrInterpolate(_ctx.msg)}</div><div${_ssrRenderAttrs(_cssVars)}>Hello World</div><div${_ssrRenderAttrs(_mergeProps({ class: "msg-class" }, _cssVars))}>${_ssrInterpolate(_ctx.msg)}</div><div${_ssrRenderAttrs(_mergeProps({class: "msg-class",msg: _ctx.msg}, _cssVars))}>闻人放歌</div><!--]-->`)
}// Check the console for the AST
  • 静态节点直接输出,绕过了vdom
  • 动态节点,还是需要动态渲染

tree-shaking

观察两者编译结果区别:

<div v-if="msg">Hello World</div>
<input v-model="msg">
// 编译结果
import { openBlock as _openBlock, createElementBlock as _createElementBlock, createCommentVNode as _createCommentVNode, vModelText as _vModelText, createElementVNode as _createElementVNode, withDirectives as _withDirectives, Fragment as _Fragment } from "vue"export function render(_ctx, _cache, $props, $setup, $data, $options) {return (_openBlock(), _createElementBlock(_Fragment, null, [(_ctx.msg)? (_openBlock(), _createElementBlock("div", { key: 0 }, "Hello World")): _createCommentVNode("v-if", true),_withDirectives(_createElementVNode("input", {"onUpdate:modelValue": $event => ((_ctx.msg) = $event)}, null, 8 /* PROPS */, ["onUpdate:modelValue"]), [[_vModelText, _ctx.msg]])], 64 /* STABLE_FRAGMENT */))
}// Check the console for the AST

<div v-if="msg">Hello World</div>// 编译结果
import { openBlock as _openBlock, createElementBlock as _createElementBlock, createCommentVNode as _createCommentVNode } from "vue"export function render(_ctx, _cache, $props, $setup, $data, $options) {return (_ctx.msg)? (_openBlock(), _createElementBlock("div", { key: 0 }, "Hello World")): _createCommentVNode("v-if", true)
}// Check the console for the AST

编译时,根据不同的情况,引入不同的API

http://www.lryc.cn/news/404959.html

相关文章:

  • 人工智能与社交变革:探索Facebook如何领导智能化社交平台
  • 八股文之java基础
  • 深度挖掘行情接口:股票市场中的关键金融数据API接口解析
  • 逆向破解 对汇编的 简单思考
  • 搜维尔科技:人机交互学术应用概览
  • 植物遗传转化相关介绍【卡梅德生物】
  • 0711springNews新闻系统管理 实现多级评论
  • 如何在Ubuntu上安装并启动SSH服务(Windows连接)
  • docker build时的网络问题
  • Vue的安全性:防范XSS攻击与安全最佳实践
  • ARM架构(一)—— ARMV8V9基础概念
  • 如何使用Python进行数据分析
  • Python学习笔记40:游戏篇之外星人入侵(一)
  • R的数据集读取和利用,如何高效地直接复制黏贴数据到R
  • @JsonProperty 踩坑
  • 业务架构、数据架构、应用架构和技术架构分析
  • android studio中svn的使用
  • 敏捷CSM认证:精通敏捷Scum估算方法,高效完成项目!
  • 三、建造者模式
  • MySQL-----索引
  • Webpack 5 Tree Shaking与Module Federation
  • 免费分享一套微信小程序图书馆座位预约管理系统(SpringBoot后端+Vue管理端)【论文+源码+SQL脚本】,帅呆了~~
  • k8s入门:从安装到实际应用
  • 基于Qt的上位机通用框架
  • Vulnhub靶场DC-7练习
  • 吴恩达深度学习笔记1 Neural Networks and Deep Learning
  • (十)Spring教程——Spring配置概述
  • 飞书群聊机器人自定义机器人接入,并实现艾特@群成员功能
  • CrowdStrike更新致850万Windows设备宕机,微软紧急救火!
  • 银行黄金交易流程