Home > Web Front-end > Vue.js > Let's talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

Let's talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

青灯夜游
Release: 2022-07-18 19:56:55
forward
1914 people have browsed it

This article will take you to understand the double-ended diff algorithm in Vue2, and talk about how Vue2 updates nodes. I hope it will be helpful to everyone!

Let's talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

Today we will talk about Vue2’s double-ended diff algorithm. (Learning video sharing: vue video tutorial)

Why do you want to talk? The most straightforward reason is the interview test. When the interviewer asks you about the role of the key, nine out of ten people will quote to the diff algorithm.

There is no way, we can only learn it when asked in the interview. Fortunately, it is not that difficult, just follow me and take a look.

For reasons of space, this article will not introduce how the virtual DOM tree is generated. It will only explain how to compare the two virtual DOM trees and update the real DOM when the data is updated. It mainly implements # in the Vue2 source code. ##patchVnode, updateChildren Function

Preliminary knowledge

##diff algorithm function

Before talking about the diff algorithm, you need to understand what it does.

We know that when the web page is running, we change some data, which may affect the DOM tree. How to display the latest data on the page? The simplest way is to push the entire tree to rebuild. Of course, this will cause a lot of waste, so Vue uses virtual DOM to save the state of the DOM tree in the page. After the data changes, build a Create a new virtual DOM tree, find the differences between the two trees, and update the real DOM in a targeted manner.

How to find the difference between the two trees and reduce the destruction and reconstruction of DOM elements is the role of the diff algorithm

Virtual DOM

Virtual DOM, also known as virtual node (vnode), is simply an object containing DOM element information. It is generally created by the

h

function. The following object can be regarded as a virtual node<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:html;toolbar:false;">const vnode = { tag: &amp;#39;div&amp;#39;, // 标签类型 text: &amp;#39;&amp;#39;, // 文本内容 children: undefined, // 子节点 }</pre><div class="contentsignin">Copy after login</div></div>For this HTML

<div>
    <p>a</p>
    <p>b</p>
    <p>c</p>
</div>
Copy after login

The conversion to vnode is like this

const vnode = {
    tag: &#39;div&#39;, // 标签类型
    text: undefined, // 文本内容
    children: [ // 子节点
        {
            tag: &#39;p&#39;,
            text: &#39;a&#39;,
            children: undefined,
        },
        {
            tag: &#39;p&#39;,
            text: &#39;b&#39;,
            children: undefined,
        },
        {
            tag: &#39;p&#39;,
            text: &#39;c&#39;,
            children: undefined,
        },
    ],
}
Copy after login

Because we need to operate the real DOM through virtual nodes, there is an elm attribute on the vnode that points to the real DOM element. And in the subsequent diff algorithm, a key will be used to uniquely identify the node, so the vnode below is such an object

const vnode = {
    tag: &#39;div&#39;, // 标签类型
    text: &#39;&#39;, // 文本内容
    children: undefined, // 子节点
    elm: undefined, // 对应的真实DOM
    key: &#39;&#39;, // 唯一标识
}
Copy after login

Vue's virtual node also has many attributes, but it is different from the diff algorithm It's irrelevant, so I won't list them

Just to explain, the text and children of the virtual node will not have values ​​at the same time. In the case of children attribute, the content in text will be converted into a text node and placed in the children array

Preparatory function

In order to make it easier to wait The code implementation is simpler. We prepare a few functions. The functions are not difficult. We paste the code directly.

The first thing we need is a function that converts virtual nodes into real DOM

// 根据虚拟节点创建真实节点
function createElement(vnode) {
    const dom = document.createElement(vnode.tag)
    if (vnode.children) {
        // 包含子节点,递归创建
        for (let i = 0; i < vnode.children.length; i++) {
            const childDom = createElement(vnode.children[i])
            dom.appendChild(childDom)
        }
    } else {
        // 内部是文字
        dom.innerHTML = vnode.text
    }
    // 补充elm属性
    vnode.elm = dom
    return dom
}
Copy after login

and three Tool function

// 判断是否未定义
function isUndef(v) {
    return v === undefined || v === null
}
// 判断是否已定义
function isDef(v) {
    return v !== undefined && v !== null
}
// 检查是否可复用
function checkSameVnode(a, b) {
    return a.tag === b.tag && a.key === b.key
}
Copy after login

patchVnode

When the data is updated, Vue creates a new vnode, and then executes the

patchVnode

function to compare the new and old ones. The difference between the virtual nodes is then processed according to the situation <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">function patchVnode(newVnode, oldVnode) {}</pre><div class="contentsignin">Copy after login</div></div> First determine whether the old and new virtual nodes are the same object. If so, there is no need to process

if (oldVnode === newVnode) return
Copy after login

Then the DOM elements of the old node are Assign to the new node and get the children attribute of the old and new nodes

let elm = (newVnode.elm = oldVnode.elm)
let oldCh = oldVnode.children
let newCh = newVnode.children
Copy after login

You can assign values ​​directly here because the tags and keys of the old and new nodes that call patchVnode must be the same, which will be explained below

Then decide how to update the DOM based on the contents of the two nodes

1. The contents of the old and new nodes are both text. Just modify the text

if (isUndef(oldCh) && isUndef(newCh)) {
    if (newVnode.text !== oldVnode.text) {
        elm.innerText = newVnode.text
    }
}
Copy after login

2. The old node has child nodes, and the content of the new node is text. Clear the old node content and change it to text

if (isDef(oldCh) && isUndef(newCh)) {
    elm.innerHTML = newVnode.text
}
Copy after login

3. The old node content is text, and the new node has child nodes. Clear the content of the old node, traverse the new node to generate child DOM elements and insert them into the node

if (isUndef(oldCh) && isDef(newCh)) {
    elm.innerHTML = &#39;&#39;
    for (let i = 0, n = newCh.length; i < n; i++){
        elm.appendChild(createElement(newCh[i]))
    }
}
Copy after login

4. The old and new nodes have child nodes. Call

updateChildren

to process. This function will be explained in the next chapter. <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">if (isDef(oldCh) &amp;&amp; isDef(newCh)) { updateChildren(elm, oldCh, newCh) }</pre><div class="contentsignin">Copy after login</div></div>Case 4 can be processed in the same way as case 3, clear the old nodes, and then traverse to generate a new DOM. But we need to know that creating DOM elements is a very time-consuming task, and the old and new child nodes are the same most of the time. If they can be reused, our performance will be greatly optimized.

So how do we determine whether a node can be reused?

This requires Vue users to help. Users define key attributes on nodes and tell Vue which nodes can be reused

As long as the label type and key value are equal , it means that the current element can be reused

However, in our project, the key is generally only set in v-for, and the key is not set in other nodes

In fact

Nodes with no key set, their key values ​​are equal by default

This is also true. Most elements in our project can be reused. Only the child elements generated by v-for depend on The array may undergo some complex changes, so the key value needs to be clearly marked to help Vue reuse nodes as much as possible.

patchVnode 的内容当然不止这些,还有样式、类名、props等数据的对比更换,篇幅原因本文将其省略了。

updateChildren

为什么采用双端 diff

好了,Vue 的使用者为每个节点的设置了 key,我们要如何从老节点中找到 key 相等的节点复用元素呢?

简单的方式就是穷举遍历,对于每个新节点的 key 遍历所有老节点,找到了就移动到首位,没找到就创建添加。

然而这明显有优化的空间,Vue 实现这部分功能时借鉴了 snabbdom 的双端 diff 算法,因为此算法将我们平时操作数组常见的 4 种情况抽离了出来,涵盖了我们业务中的大多数场景,将 O(n2) 的时间复杂度降到了 O(n)

接下来我们来学习这是如何实现的

函数实现

函数实现较为复杂,我直接把完整的代码放上来,再带领大家一段段解读

// 三个参数为:父DOM元素,旧的子节点数组,新的子节点数组
function updateChildren(parentElm, oldCh, newCh) {
    // 旧前索引
    let oldStartIdx = 0
    // 新前索引
    let newStartIdx = 0
    // 旧后索引
    let oldEndIdx = oldCh.length - 1
    // 新后索引
    let newEndIdx = newCh.length - 1
    // 四个索引对应节点
    let oldStartVnode = oldCh[0]
    let newStartVnode = newCh[0]
    let oldEndVnode = oldCh[oldEndIdx]
    let newEndVnode = newCh[newEndIdx]

    let keyMap

    // 开始循环
    while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
        // 跳过空节点 (和最后一种情况有关)
        if (isUndef(oldStartVnode)) {
            oldStartVnode = oldCh[++oldStartIdx]
        } else if (isUndef(oldEndVnode)) {
            oldEndVnode = oldCh[--oldEndIdx]
        } else if (checkSameVnode(oldStartVnode, newStartVnode)) {
            // 情况1
            // 旧前和新前相等,不需要移动
            patchVnode(newStartVnode, oldStartVnode)
            oldStartVnode = oldCh[++oldStartIdx]
            newStartVnode = newCh[++newStartIdx]
        } else if (checkSameVnode(oldEndVnode, newEndVnode)) {
            // 情况2
            // 旧后和新后相等,也不需要移动
            patchVnode(newEndVnode, oldEndVnode)
            oldEndVnode = oldCh[--oldEndIdx]
            newEndVnode = newCh[--newEndIdx]
        } else if (checkSameVnode(oldStartVnode, newEndVnode)) {
            // 情况3
            // 旧前和新后相等
            // 旧序列的第一个节点,变成了新序列的最后一个节点
            // 需要将这个节点移动到旧序列最后一个节点的后面
            // 也就是最后一个节点的下一个节点的前面
            parentElm.insertBefore(oldStartVnode.elm, oldEndVnode.elm.nextSibling)
            patchVnode(newEndVnode, oldStartVnode)
            oldStartVnode = oldCh[++oldStartIdx]
            newEndVnode = newCh[--newEndIdx]
        } else if (checkSameVnode(oldEndVnode, newStartVnode)) {
            // 情况4
            // 旧后和新前相等
            // 旧序列的最后一个节点,变成了新序列的第一个节点
            // 需要将这个节点移动到旧序列第一个节点的前面
            parentElm.insertBefore(oldEndVnode.elm, oldStartVnode.elm)
            patchVnode(newStartVnode, oldEndVnode)
            oldEndVnode = oldCh[--oldEndIdx]
            newStartVnode = newCh[++newStartIdx]
        } else {
            // 以上四种情况都不符合
            // 制作旧节点key的映射对象
            // 键为 key,值为 索引
            if (!keyMap) {
                keyMap = {}
                for (let i = oldStartIdx; i <= oldEndIdx; i++) {
                    keyMap[oldCh[i].key] = i
                }
            }
            // 寻找当前新节点在keyMap中映射的位置序号
            const idxInOld = keyMap[newStartVnode.key]
            if (isUndef(idxInOld)) {
                // 没有找到,表示他是全新的项
                // 转化为DOM节点,加入旧序列第一个节点的前面
                parentElm.insertBefore(createElement(newStartVnode), oldStartVnode.elm)
            } else {
                // 不是全新的项,需要移动
                const oldVnode = oldCh[idxInOld]
                // 移动到旧序列第一个节点之前
                parentElm.insertBefore(oldVnode.elm, oldStartVnode.elm)
                patchVnode(oldVnode, newStartVnode)
                // 把这项设置成空,循环时遇到时跳过
                oldCh[idxInOld] = undefined
            }
            // 当前新节点处理完毕,下一轮循环处理下一个新节点
            newStartVnode = newCh[++newStartIdx]
        }
    }

    // 循环结束了,start还是比end小,说明有节点没有处理到
    if (newStartIdx <= newEndIdx) {
        // 新节点没有处理到,则创建按DOM添加到新序列最后一个节点的前面
        for (let i = newStartIdx; i <= newEndIdx; i++) {
            // insertBefore方法传入null则添加到队尾
            const before = newCh[newEndIdx + 1]?.elm || null
            parentElm.insertBefore(createElement(newCh[i]), before)
        }
    } else if (oldStartIdx <= oldEndIdx) {
        // 旧节点没有处理到,删除
        for (let i = oldStartIdx; i <= oldEndIdx; i++) {
            parentElm.removeChild(oldCh[i].elm)
        }
    }
}
Copy after login

代码注释中及下文的新/旧序列,仅包含从新/旧开始索引到结束索引间的节点,也就是还未处理的节点序列,而不是整个子节点数组。

根据例子讲解

我们以下图的例子,来讲解这个函数的运行流程(方框中的内容为子节点的 key,所有节点标签相同)

首先定义了 8 个变量,表示新旧序列的开始和结束位置的索引与节点

Lets talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

然后开始循环,初始时节点都不为空

第一次循环命中情况 1,旧前与新前(key)相等

这表示旧序列的第一个节点到新序列仍是第一个节点,也就不需要移动,但还需要比较一下节点的内容有没有改变(patchVnode),并且让新旧开始索引都前进一步

// 比较节点的数据及子节点,并且将旧节点的DOM赋给新节点
patchVnode(newStartVnode, oldStartVnode)
oldStartVnode = oldCh[++oldStartIdx]
newStartVnode = newCh[++newStartIdx]
Copy after login

Lets talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

情况 1 是业务中最常见的,表示从前至后两两比较。一般把商品添加到购物车末尾,或是没有设置 key 值的子节点,都是依靠情况 1 把可复用的节点筛选完毕。

第二次循环命中情况 2,旧后和新后相等

这表示序列的末尾节点到新序列仍是末尾节点,也不需要移动,然后让新旧结束索引都后退一步

patchVnode(newEndVnode, oldEndVnode)
oldEndVnode = oldCh[--oldEndIdx]
newEndVnode = newCh[--newEndIdx]
Copy after login

Lets talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

情况 2 是情况 1 的补充,表示从后向前两两比较。有时会把新发布的评论插到开头,或者从购物车删除了一些商品,这时仅依靠情况 1 就无法迅速的筛选可复用节点,所以需要从后向前比较来配合。

第三次循环命中情况 3,旧前和新后相等

这表示旧序列的第一个节点,变成了新序列的最后一个节点。需要将这个节点移动到序列的末尾,也就是旧序列末尾节点的下一个节点(节点 e)的前面

parentElm.insertBefore(oldStartVnode.elm, oldEndVnode.elm.nextSibling)
Copy after login
Copy after login

Lets talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

然后比较新旧节点,修改索引

patchVnode(newEndVnode, oldStartVnode)
oldStartVnode = oldCh[++oldStartIdx]
newEndVnode = newCh[--newEndIdx]
Copy after login

Lets talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

情况 3 主要处理数组反转的情况,比如升序改降序,每个起始节点被移动到了末尾的位置,使用此情况将它们重新排序。

第四次循环命中情况 4,旧后与新前相等

这表示旧序列的最后一个节点,变成了新序列的第一个节点。需要将这个节点移动到序列的开头,也就是旧序列开始节点(节点 c)的前面

parentElm.insertBefore(oldStartVnode.elm, oldEndVnode.elm.nextSibling)
Copy after login
Copy after login

Lets talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

到这里说一下,图上标注的是节点 a 的后面,是因为节点 b 被移动到了末尾

节点的移动都是根据旧节点来定位的,如果想把一个节点放到序列的开头,就放到旧序列开始节点的前面;如果想把一个节点放到序列的末尾,就要放到旧序列结束节点的下一个节点的前面

然后也是比较新旧节点,修改索引,之后是下图情况

patchVnode(newStartVnode, oldEndVnode)
oldEndVnode = oldCh[--oldEndIdx]
newStartVnode = newCh[++newStartIdx]
Copy after login

Lets talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

情况 4 是情况 3 的补充,避免反转数组后又插入/删除了节点导致情况 3 无法匹配,本例就是这个情况。

第五次循环,4 种情况均为未命中

很遗憾,无法迅速锁定节点的位置,只能用传统的方式进行遍历

我们这里选择了以空间换时间的方式,定义了 keyMap,将旧序列节点的 key 与索引存起来,然后使用新开始节点的 key 去查找。

如果没找到,说明这是一个新节点,创建节点并放到开头,也就是插入到旧序列开始节点的前面

但如果找到了,则同样移动节点到序列开头,然后将对应的旧节点索引置空,在以后循环遇到空的旧节点就跳过了

本例中是未找到的情况,此节点处理完毕,新开始索引加一,超过了新结束索引,不满足循环条件,退出循环

Lets talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

然而,节点 c 并没有被处理,此时的 DOM 序列为:a,d,f,c,b,e

所以在循环之后,要检测是否有未处理的节点,如果是旧节点未处理,删除即可;

如果是新节点未处理,则创建新节点插入到旧序列的末尾或者旧序列的开头,二者其实是一个位置

我们假设旧节点中没有 c,则在第四次循环后就会出现以下情况(第四次循环命中的是情况 1)

Lets talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

如果把 f 放到序列的开头,就是旧开始节点(节点 e)的前面

而如果把 f 放到序列的末尾,也就是旧结束节点的下一个节点(节点 e)的前面

此时旧序列就是一个点,不分开头和结尾,只要保证新增节点序列按序添加就好了

至此,双端 diff 算法就讲完了

Vue 中的 key

学完 diff 算法,再聊聊 key 的作用

v-for 中的 key

上面讲的都是有 key 情况下,diff 算法能够迅速找到新旧序列中的同一节点,以较小的代价完成更新。

而如果在 v-for 中不设置 key 呢?

假设我们在数组头部插入了一个新节点,然后开始循环,每次循环都命中情况 1,尝试“复用”此节点。

但是,在对比新旧节点的内容时,都会发现内容不同,需要用新节点的内容替换旧节点。这只是复用了 DOM 的外壳,节点的内容、数据以及该节点的子节点全都要更改。

相比有 key 时的只添加一个新节点,无 key 则将所有节点都修改一遍。

v-if 自带 key

v-for 以外的元素我们一般是不设置 key 的,但是如果子元素中有 v-if 的话,就像下面这个场景(abcd是内容,并不是 key),更新子元素又会复现上一节的情况。

Lets talk about the double-ended diff algorithm in Vue2 and see how to update nodes!

然而 Vue 官方也考虑到了这点,会为 v-if 的元素加上利用 hash 函数生成的唯一 key

// 以下出自 v2 源码
var needsKey = !!el.if 
……
needsKey ? &#39;,null,false,&#39; + hash(generatedSlots) : &#39;&#39;
Copy after login

key 的另一个用法

顺便再提一嘴,key 可以绑到任意元素上,当 key 发生变化时,会导致 DOM 的销毁与重建,一般用来重复触发动画或生命周期钩子。

详情可看官方链接

https://cn.vuejs.org/v2/api/#key

结语

不记得这是第几次梳理双端 diff 的逻辑了,很早之前就已经拥抱 v3 了,把 v2 的响应式原理和 diff 算法总结一遍也算是给 v2 画上句号了。

没想到这篇文章写了 4000 多字,为此还特意去翻看了 v2 的源码。本文的代码和 v2 差别还是蛮大的,主要参考的是 snabbdom 和以前的教程,加了点自己的风格将 patchVnodeupdateChildren 实现了一遍。

接下来就能心无旁骛地看 v3 的源码了……

整理不易,如果有所帮助的话,希望能点赞关注,鼓励一下作者。

原文地址:https://juejin.cn/post/7120919895713251335

作者:清隆

【相关视频教程推荐:vuejs入门教程web前端入门

The above is the detailed content of Let's talk about the double-ended diff algorithm in Vue2 and see how to update nodes!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template