From 2f6ac67c6a5e932cf1d5bfac6750e40dc2669cfe Mon Sep 17 00:00:00 2001 From: daiwei Date: Tue, 16 Sep 2025 15:01:29 +0800 Subject: [PATCH] perf: don't initialize hydration state if no anchor --- packages/runtime-vapor/src/insertionState.ts | 30 +++++++++----------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/packages/runtime-vapor/src/insertionState.ts b/packages/runtime-vapor/src/insertionState.ts index 0b29f59d2..3a28ac08a 100644 --- a/packages/runtime-vapor/src/insertionState.ts +++ b/packages/runtime-vapor/src/insertionState.ts @@ -30,18 +30,23 @@ export function setInsertionState( anchor?: Node | 0 | null | number, ): void { 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 { - cacheTemplateChildren(anchor, parent) + insertionAnchor = undefined } } -function initializeHydrationState( - anchor: number | Node | null | undefined, - parent: ParentNode, -) { - insertionAnchor = anchor as Node +function initializeHydrationState(parent: ParentNode) { if (!hydrationStateCache.has(parent)) { const childNodes = parent.childNodes const len = childNodes.length @@ -90,14 +95,7 @@ function initializeHydrationState( } } -function cacheTemplateChildren( - anchor: number | Node | null | undefined, - parent: InsertionParent, -) { - // special handling append anchor value to null - insertionAnchor = - typeof anchor === 'number' && anchor > 0 ? null : (anchor as Node) - +function cacheTemplateChildren(parent: InsertionParent) { if (!parent.$children) { const nodes = parent.childNodes const len = nodes.length