react 源码分析之 diff 算法
//对比两棵树
function diff(oldTree, newTree){
// 当前节点的标志, 以后每遍历到一个节点, 加 1
var index = 0;
var patches = {} // 用来记录每个节点差异的对象
dfsWalk(oldTree, newTree,index,patches)
return patches
}
// 对两棵树进行深度优先遍历
function dfsWalk(onldNode , newNode , index, patches){
// 对比 oldNode 和 newNode 的不同, 记录下来
patches[index] = [...]
diffChildren(oldNode.children, newNode.children, index, patches)
}
//遍历子节点
function diffChildren(oldChildren, newChildren, index,patches){
var leftNode = null
var currentNodeIndex = index;
oldChildren.forEach(function (child, i){
var newChild = newChildren[i]
currentNodeIndex = (leftNode && leftNode.count) // 计算节点的标识
? currentNodeIndex + leftNode.count + 1
: currentNodeIndex + 1
dfsWalk(child, newChild, currentNodeIndex, patches) // 深度遍历子节点
})
}最后更新于