From 014a7137f474ee97ae95f35de85ab0e948a1a89b Mon Sep 17 00:00:00 2001 From: JoostK Date: Sun, 8 Dec 2019 15:21:38 +0100 Subject: [PATCH] perf(compiler): use a shared interpolation regex (#34332) The template parser has a certain interpolation config associated with it and builds a regular expression each time it needs to extract the interpolations from an input string. Since the interpolation config is typically the default of `{{` and `}}`, the regular expression doesn't have to be recreated each time. Therefore, this commit creates only a single regular expression instance that is used for the default configuration. In a large compilation unit with big templates, computing the regular expression took circa 275ms. This change reduces this to effectively zero. PR Close #34332 --- packages/compiler/src/expression_parser/parser.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/compiler/src/expression_parser/parser.ts b/packages/compiler/src/expression_parser/parser.ts index ca5473b6bb..9e7977a80a 100644 --- a/packages/compiler/src/expression_parser/parser.ts +++ b/packages/compiler/src/expression_parser/parser.ts @@ -23,6 +23,15 @@ export class TemplateBindingParseResult { public errors: ParserError[]) {} } +const defaultInterpolateRegExp = _createInterpolateRegExp(DEFAULT_INTERPOLATION_CONFIG); +function _getInterpolateRegExp(config: InterpolationConfig): RegExp { + if (config === DEFAULT_INTERPOLATION_CONFIG) { + return defaultInterpolateRegExp; + } else { + return _createInterpolateRegExp(config); + } +} + function _createInterpolateRegExp(config: InterpolationConfig): RegExp { const pattern = escapeRegExp(config.start) + '([\\s\\S]*?)' + escapeRegExp(config.end); return new RegExp(pattern, 'g'); @@ -138,7 +147,7 @@ export class Parser { input: string, location: string, interpolationConfig: InterpolationConfig = DEFAULT_INTERPOLATION_CONFIG): SplitInterpolation |null { - const regexp = _createInterpolateRegExp(interpolationConfig); + const regexp = _getInterpolateRegExp(interpolationConfig); const parts = input.split(regexp); if (parts.length <= 1) { return null; @@ -201,7 +210,7 @@ export class Parser { private _checkNoInterpolation( input: string, location: any, interpolationConfig: InterpolationConfig): void { - const regexp = _createInterpolateRegExp(interpolationConfig); + const regexp = _getInterpolateRegExp(interpolationConfig); const parts = input.split(regexp); if (parts.length > 1) { this._reportError(