Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit 462217f

Browse files
committed
perf(scope): Cache the Scope.watch AST.
Makes NgClass 20% faster. This will effect any Scope.watch() calls. Closes #1173
1 parent 25629d8 commit 462217f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

lib/core/scope.dart

+13-3
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,14 @@ class Scope {
247247
}
248248
}
249249

250-
AST ast = rootScope._astParser(expression,
251-
formatters: formatters, collection: collection);
250+
String astKey =
251+
"${collection ? "C" : "."}${formatters == null ? "." : formatters.hashCode}$expression";
252+
AST ast = rootScope.astCache[astKey];
253+
if (ast == null) {
254+
ast = rootScope.astCache[astKey] =
255+
rootScope._astParser(expression,
256+
formatters: formatters, collection: collection);
257+
}
252258

253259
return watch = watchAST(ast, fn, canChangeModel: canChangeModel);
254260
}
@@ -559,6 +565,9 @@ class RootScope extends Scope {
559565
final ScopeDigestTTL _ttl;
560566
final VmTurnZone _zone;
561567

568+
// For Scope.watch().
569+
final Map<String, AST> astCache = new HashMap<String, AST>();
570+
562571
_FunctionChain _runAsyncHead, _runAsyncTail;
563572
_FunctionChain _domWriteHead, _domWriteTail;
564573
_FunctionChain _domReadHead, _domReadTail;
@@ -618,7 +627,7 @@ class RootScope extends Scope {
618627

619628
RootScope(Object context, Parser parser, ASTParser astParser, FieldGetterFactory fieldGetterFactory,
620629
FormatterMap formatters, this._exceptionHandler, this._ttl, this._zone,
621-
ScopeStats _scopeStats)
630+
ScopeStats _scopeStats, CacheRegister cacheRegister)
622631
: _scopeStats = _scopeStats,
623632
_parser = parser,
624633
_astParser = astParser,
@@ -632,6 +641,7 @@ class RootScope extends Scope {
632641
{
633642
_zone.onTurnDone = apply;
634643
_zone.onError = (e, s, ls) => _exceptionHandler(e, s);
644+
cacheRegister.registerCache("ScopeWatchASTs", astCache);
635645
}
636646

637647
RootScope get rootScope => this;

0 commit comments

Comments
 (0)