Browse Source

perf(compiler-vapor): avoid redundant text run slicing

Collect adjacent interpolation text with an index walk instead of slicing the
remaining sibling list before finding the text run.

This keeps transformText scoped to the nodes that actually participate in the
merged text binding, while preserving the existing leading-text merge behavior.
daiwei 5 days ago
parent
commit
dd54ec1de0
1 changed files with 20 additions and 11 deletions
  1. 20 11
      packages/compiler-vapor/src/transforms/transformText.ts

+ 20 - 11
packages/compiler-vapor/src/transforms/transformText.ts

@@ -121,17 +121,7 @@ export const transformText: NodeTransform = (node, context) => {
 
 function processInterpolation(context: TransformContext<InterpolationNode>) {
   const parentNode = context.parent!.node
-  const children = parentNode.children
-  const nexts = children.slice(context.index)
-  const idx = nexts.findIndex(n => !isTextLike(n))
-  const nodes = (idx > -1 ? nexts.slice(0, idx) : nexts) as Array<TextLike>
-
-  // merge leading text
-  const prev = children[context.index - 1]
-  if (prev && prev.type === NodeTypes.TEXT) {
-    nodes.unshift(prev)
-  }
-  const values = processTextLikeChildren(nodes, context)
+  const values = processTextLikeChildren(collectAdjacentText(context), context)
 
   if (values.length === 0 && parentNode.type !== NodeTypes.ROOT) {
     return
@@ -176,6 +166,25 @@ function processInterpolation(context: TransformContext<InterpolationNode>) {
   })
 }
 
+function collectAdjacentText(
+  context: TransformContext<InterpolationNode>,
+): TextLike[] {
+  const children = context.parent!.node.children
+  const nodes: TextLike[] = []
+  // Include leading text that belongs to the same text run.
+  const prev = children[context.index - 1]
+  let index =
+    prev && prev.type === NodeTypes.TEXT ? context.index - 1 : context.index
+
+  for (; index < children.length; index++) {
+    const child = children[index]
+    if (!isTextLike(child)) break
+    nodes.push(child)
+  }
+
+  return nodes
+}
+
 function processTextContainer(
   children: TextLike[],
   context: TransformContext<ElementNode>,