Skip to content

Commit 504eb5c

Browse files
JoviDeCroock43081j
andauthored
perf: reduce some repeated logic (#4814) (#4821)
* perf: use createElement when cloning To avoid duplicating logic, we can use `createElement` with pre-populated props. * chore: reduce search logic * perf: reuse matched flag * chore: put the function inline again * perf: simplify to a ternary * perf: revert clone function --------- Co-authored-by: James Garbutt <[email protected]>
1 parent ee12c20 commit 504eb5c

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

src/diff/children.js

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ function findMatchingIndex(
407407
const key = childVNode.key;
408408
const type = childVNode.type;
409409
let oldVNode = oldChildren[skewedIndex];
410+
const matched = oldVNode != NULL && (oldVNode._flags & MATCHED) == 0;
410411

411412
// We only need to perform a search if there are more children
412413
// (remainingOldChildren) to search. However, if the oldVNode we just looked
@@ -421,45 +422,26 @@ function findMatchingIndex(
421422
// we should not search as we risk re-using state of an unrelated VNode. (reverted for now)
422423
let shouldSearch =
423424
// (typeof type != 'function' || type === Fragment || key) &&
424-
remainingOldChildren >
425-
(oldVNode != NULL && (oldVNode._flags & MATCHED) == 0 ? 1 : 0);
425+
remainingOldChildren > (matched ? 1 : 0);
426426

427427
if (
428428
(oldVNode === NULL && childVNode.key == null) ||
429-
(oldVNode &&
430-
key == oldVNode.key &&
431-
type == oldVNode.type &&
432-
(oldVNode._flags & MATCHED) == 0)
429+
(matched && key == oldVNode.key && type == oldVNode.type)
433430
) {
434431
return skewedIndex;
435432
} else if (shouldSearch) {
436433
let x = skewedIndex - 1;
437434
let y = skewedIndex + 1;
438435
while (x >= 0 || y < oldChildren.length) {
439-
if (x >= 0) {
440-
oldVNode = oldChildren[x];
441-
if (
442-
oldVNode &&
443-
(oldVNode._flags & MATCHED) == 0 &&
444-
key == oldVNode.key &&
445-
type == oldVNode.type
446-
) {
447-
return x;
448-
}
449-
x--;
450-
}
451-
452-
if (y < oldChildren.length) {
453-
oldVNode = oldChildren[y];
454-
if (
455-
oldVNode &&
456-
(oldVNode._flags & MATCHED) == 0 &&
457-
key == oldVNode.key &&
458-
type == oldVNode.type
459-
) {
460-
return y;
461-
}
462-
y++;
436+
const childIndex = x >= 0 ? x-- : y++;
437+
oldVNode = oldChildren[childIndex];
438+
if (
439+
oldVNode != NULL &&
440+
(oldVNode._flags & MATCHED) == 0 &&
441+
key == oldVNode.key &&
442+
type == oldVNode.type
443+
) {
444+
return childIndex;
463445
}
464446
}
465447
}

0 commit comments

Comments
 (0)