Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 842f94c

Browse files
committed
perf(vapor): improve component instantiation by using class
Mounting 10k components went from ~100ms to ~60ms with this change.
1 parent f0361ba commit 842f94c

File tree

8 files changed

+114
-168
lines changed

8 files changed

+114
-168
lines changed

packages/runtime-vapor/src/apiCreateComponent.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
type Component,
3-
type ComponentInternalInstance,
4-
createComponentInstance,
3+
ComponentInternalInstance,
54
currentInstance,
65
} from './component'
76
import { setupComponent } from './apiRender'
@@ -24,7 +23,7 @@ export function createComponent(
2423
return fallbackComponent(comp, rawProps, slots, current, singleRoot)
2524
}
2625

27-
const instance = createComponentInstance(
26+
const instance = new ComponentInternalInstance(
2827
comp,
2928
singleRoot ? withAttrs(rawProps) : rawProps,
3029
slots,
@@ -42,7 +41,7 @@ export function createComponent(
4241
setupComponent(instance)
4342

4443
// register sub-component with current component for lifecycle management
45-
current.comps.add(instance)
44+
// current.comps.add(instance)
4645

4746
return instance
4847
}

packages/runtime-vapor/src/apiCreateFor.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import {
1313
} from './dom/element'
1414
import { type Block, type Fragment, fragmentKey } from './block'
1515
import { warn } from './warning'
16-
import { currentInstance } from './component'
17-
import { componentKey } from './component'
16+
import { currentInstance, isVaporComponent } from './component'
1817
import type { DynamicSlot } from './componentSlots'
1918
import { renderEffect } from './renderEffect'
2019

@@ -382,7 +381,7 @@ function normalizeAnchor(node: Block): Node {
382381
return node
383382
} else if (isArray(node)) {
384383
return normalizeAnchor(node[0])
385-
} else if (componentKey in node) {
384+
} else if (isVaporComponent(node)) {
386385
return normalizeAnchor(node.block!)
387386
} else {
388387
return normalizeAnchor(node.nodes!)

packages/runtime-vapor/src/apiCreateVaporApp.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { NO, getGlobalThis, isFunction, isObject } from '@vue/shared'
22
import {
33
type Component,
4-
type ComponentInternalInstance,
5-
createComponentInstance,
4+
ComponentInternalInstance,
65
validateComponentName,
76
} from './component'
87
import { warn } from './warning'
@@ -126,7 +125,7 @@ export function createVaporApp(
126125
container.textContent = ''
127126
}
128127

129-
instance = createComponentInstance(
128+
instance = new ComponentInternalInstance(
130129
rootComponent,
131130
rootProps,
132131
null,

packages/runtime-vapor/src/apiRender.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import {
22
type ComponentInternalInstance,
3-
componentKey,
43
createSetupContext,
54
getAttrsProxy,
65
getSlotsProxy,
6+
isVaporComponent,
77
setCurrentInstance,
88
validateComponentName,
99
} from './component'
@@ -60,9 +60,9 @@ export function setupComponent(instance: ComponentInternalInstance): void {
6060
if (
6161
stateOrNode &&
6262
(stateOrNode instanceof Node ||
63+
isVaporComponent(stateOrNode) ||
6364
isArray(stateOrNode) ||
64-
fragmentKey in stateOrNode ||
65-
componentKey in stateOrNode)
65+
fragmentKey in stateOrNode)
6666
) {
6767
block = stateOrNode
6868
} else if (isObject(stateOrNode)) {

packages/runtime-vapor/src/block.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isArray } from '@vue/shared'
2-
import { type ComponentInternalInstance, componentKey } from './component'
2+
import { type ComponentInternalInstance, isVaporComponent } from './component'
33

44
export const fragmentKey: unique symbol = Symbol(__DEV__ ? `fragmentKey` : ``)
55

@@ -17,7 +17,7 @@ export function normalizeBlock(block: Block): Node[] {
1717
nodes.push(block)
1818
} else if (isArray(block)) {
1919
block.forEach(child => nodes.push(...normalizeBlock(child)))
20-
} else if (componentKey in block) {
20+
} else if (isVaporComponent(block)) {
2121
nodes.push(...normalizeBlock(block.block!))
2222
} else if (block) {
2323
nodes.push(...normalizeBlock(block.nodes))
@@ -34,7 +34,7 @@ export function findFirstRootElement(
3434
}
3535

3636
export function getFirstNode(block: Block | null): Node | undefined {
37-
if (!block || componentKey in block) return
37+
if (!block || isVaporComponent(block)) return
3838
if (block instanceof Node) return block
3939
if (isArray(block)) {
4040
if (block.length === 1) {

0 commit comments

Comments
 (0)