]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
perf(compiler): pre-compute maxCRNameLength for perf
authorEvan You <yyx990803@gmail.com>
Sat, 16 Nov 2019 21:19:47 +0000 (16:19 -0500)
committerEvan You <yyx990803@gmail.com>
Sat, 16 Nov 2019 21:19:47 +0000 (16:19 -0500)
packages/compiler-core/src/parse.ts
packages/compiler-dom/src/parserOptionsStandard.ts

index 94b71be182e4fdf1ccc481590fd354404551160f..a794b3cfd810da8ff33e83a4f4453eb3535b22ca 100644 (file)
@@ -46,7 +46,10 @@ export interface ParserOptions {
 
   // Map to HTML entities. E.g., `{ "amp;": "&" }`
   // The full set is https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references
-  namedCharacterReferences?: { [name: string]: string | undefined }
+  namedCharacterReferences?: Record<string, string>
+  // this number is based on the map above, but it should be pre-computed
+  // to avoid the cost on every parse() call.
+  maxCRNameLength?: number
 
   onError?: (error: CompilerError) => void
 }
@@ -69,6 +72,7 @@ export const defaultParserOptions: MergedParserOptions = {
     'apos;': "'",
     'quot;': '"'
   },
+  maxCRNameLength: 5,
   onError: defaultOnError
 }
 
@@ -88,7 +92,6 @@ interface ParserContext {
   offset: number
   line: number
   column: number
-  maxCRNameLength: number
   inPre: boolean
 }
 
@@ -123,10 +126,6 @@ function createParserContext(
     offset: 0,
     originalSource: content,
     source: content,
-    maxCRNameLength: Object.keys(
-      options.namedCharacterReferences ||
-        defaultParserOptions.namedCharacterReferences
-    ).reduce((max, name) => Math.max(max, name.length), 0),
     inPre: false
   }
 }
@@ -839,7 +838,7 @@ function parseTextData(
         value: string | undefined = undefined
       if (/[0-9a-z]/i.test(rawText[1])) {
         for (
-          let length = context.maxCRNameLength;
+          let length = context.options.maxCRNameLength;
           !value && length > 0;
           --length
         ) {
index 652da306bbe5af65cc4aafa3956aee8bba8e617f..73841b445c3379362534b864306234f0e446a1b3 100644 (file)
@@ -7,5 +7,9 @@ export const parserOptionsStandard: ParserOptions = {
   ...parserOptionsMinimal,
 
   // https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references
-  namedCharacterReferences
+  namedCharacterReferences,
+  maxCRNameLength: Object.keys(namedCharacterReferences).reduce(
+    (max, name) => Math.max(max, name.length),
+    0
+  )
 }