Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ function constructNewChildrenArray(
* real reorder happened and we need to compute the minimal set of moves. */
let moved = false;

newParentVNode._children = new Array(newChildrenLength);
// Hoisted: every normalization branch below writes through this array, so
// reading `_children` off the vnode each time is a wasted property load.
/** @type {VNode[]} */
let newChildren = (newParentVNode._children = new Array(newChildrenLength));
for (i = 0; i < newChildrenLength; i++) {
// @ts-expect-error We are reusing the childVNode variable to hold both the
// pre and post normalized childVNode
Expand All @@ -193,7 +196,7 @@ function constructNewChildrenArray(
typeof childVNode == 'boolean' ||
typeof childVNode == 'function'
) {
newParentVNode._children[i] = NULL;
newChildren[i] = NULL;
continue;
}
// If this newVNode is being reused (e.g. <div>{reuse}{reuse}</div>) in the same diff,
Expand All @@ -206,15 +209,15 @@ function constructNewChildrenArray(
typeof childVNode != 'object' ||
childVNode.constructor == String
) {
childVNode = newParentVNode._children[i] = createVNode(
childVNode = newChildren[i] = createVNode(
NULL,
childVNode,
NULL,
NULL,
NULL
);
} else if (isArray(childVNode)) {
childVNode = newParentVNode._children[i] = createVNode(
childVNode = newChildren[i] = createVNode(
Fragment,
{ children: childVNode },
NULL,
Expand All @@ -226,15 +229,15 @@ function constructNewChildrenArray(
// scenario:
// const reuse = <div />
// <div>{reuse}<span />{reuse}</div>
childVNode = newParentVNode._children[i] = createVNode(
childVNode = newChildren[i] = createVNode(
childVNode.type,
childVNode.props,
childVNode.key,
childVNode.ref || NULL,
childVNode._original
);
} else {
newParentVNode._children[i] = childVNode;
newChildren[i] = childVNode;
}

const skewedIndex = i + skew;
Expand Down Expand Up @@ -329,7 +332,7 @@ function constructNewChildrenArray(
/** @type {number[]} length of the longest increasing subsequence ending at child i */
let lisLengths = [];
for (i = 0; i < newChildrenLength; i++) {
childVNode = newParentVNode._children[i];
childVNode = newChildren[i];
if (childVNode && childVNode._flags & MATCHED) {
// Binary search for the insertion point, keeping the pass at
// O(n log n) even for pathological reorders.
Expand Down Expand Up @@ -357,7 +360,7 @@ function constructNewChildrenArray(
if (lisLengths[i] == skew) {
skew--;
} else {
newParentVNode._children[i]._flags |= INSERT_VNODE;
newChildren[i]._flags |= INSERT_VNODE;
}
}
}
Expand Down
Loading