vue中的diff算法

本文介绍了一种用于更新用户界面的高效虚拟DOM差异比较算法。该算法通过对比新旧虚拟DOM树来确定最小化实际DOM操作的方式,从而提高前端应用的性能。具体包括:标签名变化时替换元素;属性变化时更新属性;子节点为字符串或数组时的不同处理策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function diff(n1,n2)
{
    //n1=preSubTree  n2=curSubTree
    //1.tag变化
    if(n1.tag!==n2.tag)
    {
        n1.el.replaceWith(document.createElement(n2.tag));
    }

    //2.props变化
    else
    {
        //交换新旧节点的element
        const el=(n2.el=n1.el);//el=n2.el
        const {props:newProps}=n2;
        const {props:oldProps}=n1;
        //新老节点的props数量一样的情况
        if(newProps&&oldProps)
        {
            Object.keys(newProps).forEach(key=>{
                const newValue=newProps[key];
                const oldValue=oldProps[key];
                console.log(newValue,oldValue);
                if(newValue!=oldValue)
                {
                    n1.el.setAttribute(key,newValue);
                }
            })
        }
        //新老节点的props的数量不一样
        if(oldProps)
        {
            Object.keys(oldProps).forEach(key=>{
                if(!newProps[key])
                {
                    //在老节点中删除
                    n1.el.removeAttribute(key);
                }
            })
        }
    }

    //3.children改变
    //1.newChildren-> string  (old:string   old:Array)
    //2.newChildren-> array   (old:string   old:array)
    const {children:newChildren=[]}=n2;
    const {children:oldChildren=[]}=n1;
    if(typeof newChildren=="string")
    {
        if(typeof oldChildren=="string")
        {
            if(newChildren!=oldChildren)
            {
                n2.el.textContent=oldChildren
            }
        }
        else if(Array.isArray(oldChildren))
        {
            n2.el.textContent=oldChildren
        }
    }
    else if(Array.isArray(newChildren))
    {
        if(typeof oldChildren=="string")
        {
            //清空老节点的内容
            n2.el.innerText=``;
            mountElement(n2,el);
        }
        else if(Array.isArray(oldChildren))
        {
            const length=Math.min(oldChildren.length,newChildren.length);
            for(let index=0;index<length;index++)
            {
                const newVnode=newChildren[index];
                const oldVnode=oldChildren[index];
                diff(oldVnode,newVnode);
            }
            if(newChildren.length>length)
            {
                //创建节点
                for(let index=length;index<newChildren.length;index++)
                {
                    const newVnode=newChildren[index];
                     mountElement(newVnode);
                }
            }
            if(oldChildren.length>length)
            {
                //删除节点
                for(let index=length;index<oldChildren.length;index++)
                {
                    const oldVnode=oldChildren[index];
                    oldVnode.el.parent.removeChild(oldVnode.el);
                }
            }
        }
    }
}
function mountElement(vnode,container)
{
    //vnode->tag,props,children

    //tag
    const {tag,props,children}=vnode;
    const element=(vnode.el=document.createElement(tag));

    //props
    if(props)
    {
        //一个element可能有很多的prop,需要遍历
        for(const key in props)
        {
            const value=props[key];
            element.setAttribute(key,value);
        }
    }

    //children->可以字符串和 数组类型的children
    if(typeof children ==="string")
    {
        const textNode=document.createTextNode(children);
        element.append(textNode);
    }
    //children是一个数组
    else if(Array.isArray(children))
    {
        children.forEach(vnode=>{
            //在当前的element容器中递归调用‘
            //并且挂载到当前的容器上去
            mountElement(vnode,element);
        })
    }
    container.append(element);
}
export {diff,mountElement}
Vue中的diff算法是用来比较新旧虚拟节点(VNode)的差异,并将差异应用到真实的DOM树上,以更新视图。Vuediff算法采用了双指针的方式进行比较。 首先,Vue会用JavaScript对象结构表示DOM树的结构,并根据这个树构建一个真正的DOM树,插入到文档中。当状态发生变更时,Vue会重新构建一棵新的对象树。 然后,Vue会对新旧节点进行比较(diff),记录两棵树的差异。比较的方式是通过双指针,分别指向新旧节点的开头和末尾。根据节点的类型和属性进行比较,如果节点相同,则复用旧节点;如果节点不同,则创建新的真实节点。 在比较的过程中,Vue会记录差异,并将差异应用到第一棵树所构建的真正的DOM树上(patch),从而更新视图。 总结来说,Vuediff算法通过比较新旧虚拟节点的差异,将差异应用到真实的DOM树上,以更新视图。这种算法可以高效地更新视图,减少不必要的DOM操作,提高性能。 #### 引用[.reference_title] - *1* *2* *3* [vue中的diff算法](https://blog.csdn.net/weixin_43638968/article/details/112686317)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值