perf(compiler): pre-compute maxCRNameLength for perf

This commit is contained in:
Evan You 2019-11-16 16:19:47 -05:00
parent 2780e0df4c
commit 1de072567d
2 changed files with 11 additions and 8 deletions

View File

@ -46,7 +46,10 @@ export interface ParserOptions {
// Map to HTML entities. E.g., `{ "amp;": "&" }` // Map to HTML entities. E.g., `{ "amp;": "&" }`
// The full set is https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references // 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 onError?: (error: CompilerError) => void
} }
@ -69,6 +72,7 @@ export const defaultParserOptions: MergedParserOptions = {
'apos;': "'", 'apos;': "'",
'quot;': '"' 'quot;': '"'
}, },
maxCRNameLength: 5,
onError: defaultOnError onError: defaultOnError
} }
@ -88,7 +92,6 @@ interface ParserContext {
offset: number offset: number
line: number line: number
column: number column: number
maxCRNameLength: number
inPre: boolean inPre: boolean
} }
@ -123,10 +126,6 @@ function createParserContext(
offset: 0, offset: 0,
originalSource: content, originalSource: content,
source: content, source: content,
maxCRNameLength: Object.keys(
options.namedCharacterReferences ||
defaultParserOptions.namedCharacterReferences
).reduce((max, name) => Math.max(max, name.length), 0),
inPre: false inPre: false
} }
} }
@ -839,7 +838,7 @@ function parseTextData(
value: string | undefined = undefined value: string | undefined = undefined
if (/[0-9a-z]/i.test(rawText[1])) { if (/[0-9a-z]/i.test(rawText[1])) {
for ( for (
let length = context.maxCRNameLength; let length = context.options.maxCRNameLength;
!value && length > 0; !value && length > 0;
--length --length
) { ) {

View File

@ -7,5 +7,9 @@ export const parserOptionsStandard: ParserOptions = {
...parserOptionsMinimal, ...parserOptionsMinimal,
// https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references // 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
)
} }