perf: don't initialize hydration state if no anchor

This commit is contained in:
daiwei 2025-09-16 15:01:29 +08:00
parent dd86809806
commit 2f6ac67c6a
1 changed files with 14 additions and 16 deletions

View File

@ -30,18 +30,23 @@ export function setInsertionState(
anchor?: Node | 0 | null | number, anchor?: Node | 0 | null | number,
): void { ): void {
insertionParent = parent insertionParent = parent
if (isHydrating) {
initializeHydrationState(anchor, parent) if (anchor !== undefined) {
if (isHydrating) {
insertionAnchor = anchor as Node
initializeHydrationState(parent)
} else {
// special handling append anchor value to null
insertionAnchor =
typeof anchor === 'number' && anchor > 0 ? null : (anchor as Node)
cacheTemplateChildren(parent)
}
} else { } else {
cacheTemplateChildren(anchor, parent) insertionAnchor = undefined
} }
} }
function initializeHydrationState( function initializeHydrationState(parent: ParentNode) {
anchor: number | Node | null | undefined,
parent: ParentNode,
) {
insertionAnchor = anchor as Node
if (!hydrationStateCache.has(parent)) { if (!hydrationStateCache.has(parent)) {
const childNodes = parent.childNodes const childNodes = parent.childNodes
const len = childNodes.length const len = childNodes.length
@ -90,14 +95,7 @@ function initializeHydrationState(
} }
} }
function cacheTemplateChildren( function cacheTemplateChildren(parent: InsertionParent) {
anchor: number | Node | null | undefined,
parent: InsertionParent,
) {
// special handling append anchor value to null
insertionAnchor =
typeof anchor === 'number' && anchor > 0 ? null : (anchor as Node)
if (!parent.$children) { if (!parent.$children) {
const nodes = parent.childNodes const nodes = parent.childNodes
const len = nodes.length const len = nodes.length