perf(compiler-core): add perf optimization to parseText (#458)

This commit is contained in:
Gabriel Loiácono 2019-11-15 19:48:01 -03:00 committed by Evan You
parent 353b06df77
commit 2780e0df4c

View File

@ -767,16 +767,19 @@ function parseInterpolation(
function parseText(context: ParserContext, mode: TextModes): TextNode {
__TEST__ && assert(context.source.length > 0)
const [open] = context.options.delimiters
// TODO could probably use some perf optimization
const endIndex = Math.min(
...[
context.source.indexOf('<', 1),
context.source.indexOf(open, 1),
mode === TextModes.CDATA ? context.source.indexOf(']]>') : -1,
context.source.length
].filter(n => n !== -1)
)
const endTokens = ['<', context.options.delimiters[0]]
if (mode === TextModes.CDATA) {
endTokens.push(']]>')
}
let endIndex = context.source.length
for (let i = 0; i < endTokens.length; i++) {
const index = context.source.indexOf(endTokens[i], 1)
if (index !== -1 && endIndex > index) {
endIndex = index
}
}
__TEST__ && assert(endIndex > 0)
const start = getCursor(context)