1
- import { isHydrating } from './dom/hydration'
1
+ import { isComment , isHydrating } from './dom/hydration'
2
2
export type ChildItem = ChildNode & {
3
3
$idx : number
4
4
// used count as an anchor
@@ -50,18 +50,30 @@ function initializeHydrationState(parent: ParentNode) {
50
50
if ( ! hydrationStateCache . has ( parent ) ) {
51
51
const childNodes = parent . childNodes
52
52
const len = childNodes . length
53
+
54
+ // fast path for single child case. No need to build logicalChildren
55
+ if (
56
+ len === 1 ||
57
+ ( len === 3 &&
58
+ isComment ( childNodes [ 0 ] , '[' ) &&
59
+ isComment ( childNodes [ 2 ] , ']' ) )
60
+ ) {
61
+ insertionAnchor = undefined
62
+ return
63
+ }
64
+
53
65
const logicalChildren = new Array ( len ) as ChildItem [ ]
54
66
// Build logical children:
55
67
// - static node: keep the node as a child
56
68
// - fragment: keep only the start anchor ('<!--[-->') as a child
57
69
let index = 0
58
70
for ( let i = 0 ; i < len ; i ++ ) {
59
71
const n = childNodes [ i ] as ChildItem
72
+ n . $idx = index
60
73
if ( n . nodeType === 8 ) {
61
74
const data = ( n as any as Comment ) . data
62
75
// vdom fragment
63
76
if ( data === '[' ) {
64
- n . $idx = index
65
77
logicalChildren [ index ++ ] = n
66
78
// find matching end anchor, accounting for nested fragments
67
79
let depth = 1
@@ -82,7 +94,6 @@ function initializeHydrationState(parent: ParentNode) {
82
94
continue
83
95
}
84
96
}
85
- n . $idx = index
86
97
logicalChildren [ index ++ ] = n
87
98
}
88
99
logicalChildren . length = index
0 commit comments