Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e0489ab

Browse files
committed
perf($compile): add debug classes in compile phase
In a93f03d and d37f103 we changed the compiler and ngBind to add debugging CSS classes (i.e. ng-scope, ng-binding) in linking function. This simplified the code and made sense under the original assumptions that the debug info will be disabled by default. That is however not the case - debug info is enabled by default. When debug info is enabled, this change improves the largetable-bp benchmark by ~580ms, that is 30% faster. Measuring the “create” phase, 25 loops, meantime ~1920ms -> ~1340ms. This change does not affect performance when debug info is disabled.
1 parent 2218e6f commit e0489ab

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/ng/compile.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -915,16 +915,22 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
915915
bindings.push(binding);
916916
}
917917

918-
safeAddClass($element, 'ng-binding');
919918
$element.data('$binding', bindings);
920919
} : noop;
921920

921+
compile.$$addBindingClass = debugInfoEnabled ? function $$addBindingClass($element) {
922+
safeAddClass($element, 'ng-binding');
923+
} : noop;
924+
922925
compile.$$addScopeInfo = debugInfoEnabled ? function $$addScopeInfo($element, scope, isolated, noTemplate) {
923-
safeAddClass($element, isolated ? 'ng-isolate-scope' : 'ng-scope');
924926
var dataName = isolated ? (noTemplate ? '$isolateScopeNoTemplate' : '$isolateScope') : '$scope';
925927
$element.data(dataName, scope);
926928
} : noop;
927929

930+
compile.$$addScopeClass = debugInfoEnabled ? function $$addScopeClass($element, isolated) {
931+
safeAddClass($element, isolated ? 'ng-isolate-scope' : 'ng-scope');
932+
} : noop;
933+
928934
return compile;
929935

930936
//================================
@@ -947,6 +953,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
947953
compileNodes($compileNodes, transcludeFn, $compileNodes,
948954
maxPriority, ignoreDirective, previousCompileContext);
949955

956+
compile.$$addScopeClass($compileNodes);
957+
950958
return function publicLinkFn(scope, cloneConnectFn, transcludeControllers, parentBoundTranscludeFn, futureParentElement){
951959
var namespace = null;
952960
assertArg(scope, 'scope');
@@ -1021,6 +1029,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
10211029
null, [], [], previousCompileContext)
10221030
: null;
10231031

1032+
if (nodeLinkFn && nodeLinkFn.scope) {
1033+
compile.$$addScopeClass(attrs.$$element);
1034+
}
1035+
10241036
childLinkFn = (nodeLinkFn && nodeLinkFn.terminal ||
10251037
!(childNodes = nodeList[i].childNodes) ||
10261038
!childNodes.length)
@@ -1574,8 +1586,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
15741586

15751587
compile.$$addScopeInfo($element, isolateScope, true, !(templateDirective && (templateDirective === newIsolateScopeDirective ||
15761588
templateDirective === newIsolateScopeDirective.$$originalDirective)));
1577-
1578-
safeAddClass($element, 'ng-isolate-scope');
1589+
compile.$$addScopeClass($element, true);
15791590

15801591
forEach(newIsolateScopeDirective.scope, function(definition, scopeName) {
15811592
var match = definition.match(LOCAL_REGEXP) || [],
@@ -1985,8 +1996,16 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19851996
directives.push({
19861997
priority: 0,
19871998
compile: function textInterpolateCompileFn(templateNode) {
1999+
var templateNodeParent = templateNode.parent(),
2000+
hasCompileParent = !!templateNodeParent.length;
2001+
2002+
// When transcluding a template that has bindings in the root
2003+
// we don't have a parent and thus need to add the class during linking fn.
2004+
if (hasCompileParent) compile.$$addBindingClass(templateNodeParent);
2005+
19882006
return function textInterpolateLinkFn(scope, node) {
19892007
var parent = node.parent();
2008+
if (!hasCompileParent) compile.$$addBindingClass(parent);
19902009
compile.$$addBindingInfo(parent, interpolateFn.expressions);
19912010
scope.$watch(interpolateFn, function interpolateFnWatchAction(value) {
19922011
node[0].nodeValue = value;

src/ng/directive/ngBind.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var ngBindDirective = ['$compile', function($compile) {
5555
return {
5656
restrict: 'AC',
5757
compile: function(templateElement) {
58+
$compile.$$addBindingClass(templateElement);
5859
return function (scope, element, attr) {
5960
$compile.$$addBindingInfo(element, attr.ngBind);
6061
scope.$watch(attr.ngBind, function ngBindWatchAction(value) {
@@ -121,13 +122,18 @@ var ngBindDirective = ['$compile', function($compile) {
121122
</example>
122123
*/
123124
var ngBindTemplateDirective = ['$interpolate', '$compile', function($interpolate, $compile) {
124-
return function(scope, element, attr) {
125-
var interpolateFn = $interpolate(element.attr(attr.$attr.ngBindTemplate));
126-
$compile.$$addBindingInfo(element, interpolateFn.expressions);
127-
attr.$observe('ngBindTemplate', function(value) {
128-
element.text(value);
129-
});
130-
};
125+
return {
126+
compile: function(templateElement) {
127+
$compile.$$addBindingClass(templateElement);
128+
return function(scope, element, attr) {
129+
var interpolateFn = $interpolate(element.attr(attr.$attr.ngBindTemplate));
130+
$compile.$$addBindingInfo(element, interpolateFn.expressions);
131+
attr.$observe('ngBindTemplate', function(value) {
132+
element.text(value);
133+
});
134+
};
135+
}
136+
}
131137
}];
132138

133139

@@ -180,6 +186,7 @@ var ngBindHtmlDirective = ['$sce', '$parse', '$compile', function($sce, $parse,
180186
return {
181187
restrict: 'A',
182188
compile: function (tElement, tAttrs) {
189+
$compile.$$addBindingClass(tElement);
183190

184191
return function (scope, element, attr) {
185192
$compile.$$addBindingInfo(element, attr.ngBindHtml);

0 commit comments

Comments
 (0)