vue3 源码的 diff 算法

本文深入探讨Vue3.2.31版本的源码,聚焦于diff算法,尤其是key在算法中的作用。无key时,diff效率低下,如c和d的不必要的变动;而有key时,Vue通过key优化,如a和b的匹配,c和f因key不一致导致的break。详细分析了从头到尾的遍历比较、尾部遍历、新增和移除节点等步骤,阐述了key如何提升diff效率。
摘要由CSDN通过智能技术生成

我看的是 vue3.2.31 版本的源码,diff 算法具体的位置在 packages/runtime-core/src/render.ts 文件中,源码中一些部分我已经做了注释

1、Vue源码对于key的判断

  • 下面是patchChildren方法(进行新旧VNode节点比对)的源码,其位置在vue3源码(我看的源码是vue3.2.31版本)的:packages/runtime-core/src/render.ts文件的1590-1691行

    // 进行 VNode 节点的比对
    const patchChildren: PatchChildrenFn = (
      n1,
      n2,
      container,
      anchor,
      parentComponent,
      parentSuspense,
      isSVG,
      slotScopeIds,
      optimized = false
    ) => {
      const c1 = n1 && n1.children
      const prevShapeFlag = n1 ? n1.shapeFlag : 0
      const c2 = n2.children
    
      // 这个 patchFlag,就是一个判断渲染的节点上是包含 key 的一个标识符
      const { patchFlag, shapeFlag } = n2
    
      if (patchFlag > 0) {
        if (patchFlag & PatchFlags.KEYED_FRAGMENT) {
          // 当节点上面存在 key,执行的是 patchKeyedChildren 方法
          patchKeyedChildren(
            c1 as VNode[],
            c2 as VNodeArrayChildren,
            container,
            anchor,
            parentComponent,
            parentSuspense,
            isSVG,
            slotScopeIds,
            optimized
          )
          return
        } else if (patchFlag & PatchFlags.UNKEYED_FRAGMENT) {
          // 当前节点上不存在 key,执行的是 patchUnkeyedChildren 方法
          patchUnkeyedChildren**(
            c1 as VNode[],
            c2 as VNodeArrayChildren,
            container,
            anchor,
            parentComponent,
            parentSuspense,
            isSVG,
            slotScopeIds,
            optimized
          )
          return
        }
      }
    
      // children has 3 possibilities: text, array or no children.
      if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
        // text children fast path
        if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
          unmountChildren(c1 as VNode[], parentComponent, parentSuspense)
        }
        if (c2 !== c1) {
          hostSetElementText(container, c2 as string)
        }
      } else {
        if (prevShapeFlag & ShapeFlags.ARRAY_CHILDREN) {
          // prev children was array
          if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
            // two arrays, cannot assume anything, do full diff
            patchKeyedChildren(
              c1 as VNode[],
              c2 as VNodeArrayChildren,
              container,
              anchor,
              parentComponent,
              parentSuspense,
              isSVG,
              slotScopeIds,
              optimized
            )
          } else {
            // no new children, just unmount old
            unmountChildren(c1 as VNode[], parentComponent, parentSuspense, true)
          }
        } else {
          // prev children was text OR null
          // new children is array OR null
          if (prevShapeFlag & ShapeFlags.TEXT_CHILDREN) {
            hostSetElementText(container, '')
          }
          // mount new if array
          if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
            mountChildren(
              c2 as VNodeArrayChildren,
              container,
              anchor,
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凉爽爽爽爽爽爽爽爽爽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值