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

Commit fdf9989

Browse files
jbedardIgorMinar
authored andcommitted
perf($compile): only iterate over elements with link functions
Closes #8741
1 parent addfc56 commit fdf9989

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/ng/compile.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -987,9 +987,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
987987
(nodeLinkFn.transcludeOnThisElement || !nodeLinkFn.templateOnThisElement)
988988
&& nodeLinkFn.transclude) : transcludeFn);
989989

990-
linkFns.push(nodeLinkFn, childLinkFn);
991-
linkFnFound = linkFnFound || nodeLinkFn || childLinkFn;
992-
nodeLinkFnFound = nodeLinkFnFound || nodeLinkFn;
990+
if (nodeLinkFn || childLinkFn) {
991+
linkFns.push(i, nodeLinkFn, childLinkFn);
992+
linkFnFound = true;
993+
nodeLinkFnFound = nodeLinkFnFound || nodeLinkFn;
994+
}
993995

994996
//use the previous context only for the first element in the virtual group
995997
previousCompileContext = null;
@@ -999,7 +1001,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
9991001
return linkFnFound ? compositeLinkFn : null;
10001002

10011003
function compositeLinkFn(scope, nodeList, $rootElement, parentBoundTranscludeFn) {
1002-
var nodeLinkFn, childLinkFn, node, childScope, i, ii, n, childBoundTranscludeFn;
1004+
var nodeLinkFn, childLinkFn, node, childScope, i, ii, idx, childBoundTranscludeFn;
10031005
var stableNodeList;
10041006

10051007

@@ -1009,16 +1011,17 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
10091011
var nodeListLength = nodeList.length;
10101012
stableNodeList = new Array(nodeListLength);
10111013

1012-
for (i = 0; i < nodeListLength; i++) {
1013-
stableNodeList[i] = nodeList[i];
1014+
// create a sparse array by only copying the elements which have a linkFn
1015+
for (i = 0; i < linkFns.length; i+=3) {
1016+
idx = linkFns[i];
1017+
stableNodeList[idx] = nodeList[idx];
10141018
}
10151019
} else {
10161020
stableNodeList = nodeList;
10171021
}
10181022

1019-
// TODO(perf): when the DOM is sparsely annotated with directives, we spend a lot of time iterating over nulls here
1020-
for(i = 0, n = 0, ii = linkFns.length; i < ii; n++) {
1021-
node = stableNodeList[n];
1023+
for(i = 0, ii = linkFns.length; i < ii;) {
1024+
node = stableNodeList[linkFns[i++]];
10221025
nodeLinkFn = linkFns[i++];
10231026
childLinkFn = linkFns[i++];
10241027

0 commit comments

Comments
 (0)