Skip to content

Commit

Permalink
perf(compiler): use a shared interpolation regex (#34332)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JoostK authored and kara committed Dec 12, 2019
1 parent 5d871b5 commit 940e62b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/compiler/src/expression_parser/parser.ts
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 940e62b

Please sign in to comment.