Vue3之Diff算法源码分析(1),前端高级面试题

本文详细分析了虚拟DOM(如React/Vue中的VNode)的同步更新策略,包括从头开始、从尾开始、共用序列、插入和移除子节点,以及处理未知序列的情况。技术涉及组件挂载、卸载、节点比较和优化等。
摘要由CSDN通过智能技术生成

c1: VNode[],

c2: VNodeArrayChildren,

container: RendererElement,

parentAnchor: RendererNode | null,

parentComponent: ComponentInternalInstance | null,

parentSuspense: SuspenseBoundary | null,

isSVG: boolean,

slotScopeIds: string[] | null,

optimized: boolean

) => {

let i = 0

const l2 = c2.length

let e1 = c1.length - 1 // prev ending index

let e2 = l2 - 1 // next ending index

// 1. sync from start

// (a b) c

// (a b) d e

while (i <= e1 && i <= e2) {

const n1 = c1[i]

const n2 = (c2[i] = optimized

? cloneIfMounted(c2[i] as VNode)
normalizeVNode(c2[i]))

if (isSameVNodeType(n1, n2)) {

patch(

n1,

n2,

container,

null,

parentComponent,

parentSuspense,

isSVG,

slotScopeIds,

optimized

)

} else {

break

}

i++

}

// 2. sync from end

// a (b c)

// d e (b c)

while (i <= e1 && i <= e2) {

const n1 = c1[e1]

const n2 = (c2[e2] = optimized

? cloneIfMounted(c2[e2] as VNode)
normalizeVNode(c2[e2]))

if (isSameVNodeType(n1, n2)) {

patch(

n1,

n2,

container,

null,

parentComponent,

parentSuspense,

isSVG,

slotScopeIds,

optimized

)

} else {

break

}

e1–

e2–

}

// 3. common sequence + mount

// (a b)

// (a b) c

// i = 2, e1 = 1, e2 = 2

  • 10
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值