Просмотр исходного кода

perf(runtime-vapor): improve traverse children

三咲智子 Kevin Deng 2 лет назад
Родитель
Сommit
531f4f0052
2 измененных файлов с 16 добавлено и 8 удалено
  1. 0 8
      packages/runtime-vapor/src/dom.ts
  2. 16 0
      packages/runtime-vapor/src/template.ts

+ 0 - 8
packages/runtime-vapor/src/dom.ts

@@ -65,14 +65,6 @@ export function remove(block: Block, parent: ParentBlock) {
   }
 }
 
-/*! #__NO_SIDE_EFFECTS__ */
-export function children(node: Node | Node[], ...paths: number[]): Node {
-  for (const idx of paths) {
-    node = isArray(node) ? node[idx] : node.childNodes[idx]
-  }
-  return node as Node
-}
-
 /*! #__NO_SIDE_EFFECTS__ */
 export function createTextNode(val?: unknown): Text {
   // eslint-disable-next-line no-restricted-globals

+ 16 - 0
packages/runtime-vapor/src/template.ts

@@ -1,3 +1,5 @@
+import { isArray } from '@vue/shared'
+
 /*! #__NO_SIDE_EFFECTS__ */
 export function template(str: string): () => ChildNode[] {
   let cached = false
@@ -24,3 +26,17 @@ export function template(str: string): () => ChildNode[] {
 function fragmentToNodes(node: DocumentFragment): ChildNode[] {
   return Array.from((node.cloneNode(true) as DocumentFragment).childNodes)
 }
+
+/*! #__NO_SIDE_EFFECTS__ */
+export function children(node: Node | Node[], ...paths: number[]): Node {
+  for (const idx of paths) {
+    if (isArray(node)) {
+      node = node[idx]
+    } else {
+      for (let i = 0; i <= idx; i++) {
+        node = (node as Node)[i === 0 ? 'firstChild' : 'nextSibling']!
+      }
+    }
+  }
+  return node as Node
+}