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

Commit 406576a

Browse files
committed
perf($compile): only iterate over elements with link functions
1 parent fc8d6d7 commit 406576a

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

src/ng/compile.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
960960
function compileNodes(nodeList, transcludeFn, $rootElement, maxPriority, ignoreDirective,
961961
previousCompileContext) {
962962
var linkFns = [],
963-
attrs, directives, nodeLinkFn, childNodes, childLinkFn, linkFnFound, nodeLinkFnFound;
963+
attrs, directives, nodeLinkFn, childNodes, childLinkFn, nodeLinkFnFound;
964964

965965
for (var i = 0; i < nodeList.length; i++) {
966966
attrs = new Attributes();
@@ -987,38 +987,39 @@ 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+
nodeLinkFnFound = nodeLinkFnFound || nodeLinkFn;
993+
}
993994

994995
//use the previous context only for the first element in the virtual group
995996
previousCompileContext = null;
996997
}
997998

998999
// return a linking function if we have found anything, null otherwise
999-
return linkFnFound ? compositeLinkFn : null;
1000+
return linkFns.length ? compositeLinkFn : null;
10001001

10011002
function compositeLinkFn(scope, nodeList, $rootElement, parentBoundTranscludeFn) {
1002-
var nodeLinkFn, childLinkFn, node, childScope, i, ii, n, childBoundTranscludeFn;
1003+
var nodeLinkFn, childLinkFn, node, childScope, i, ii, idx, childBoundTranscludeFn;
10031004
var stableNodeList;
10041005

10051006

10061007
if (nodeLinkFnFound) {
10071008
// copy nodeList so that if a nodeLinkFn removes or adds an element at this DOM level our
10081009
// offsets don't get screwed up
1009-
var nodeListLength = nodeList.length;
1010-
stableNodeList = new Array(nodeListLength);
1010+
stableNodeList = [];
10111011

1012-
for (i = 0; i < nodeListLength; i++) {
1013-
stableNodeList[i] = nodeList[i];
1012+
// create a sparse array by only copying the elements which have a linkFn
1013+
for (i = 0, ii = linkFns.length; i < ii; i+=3) {
1014+
idx = linkFns[i];
1015+
stableNodeList[idx] = nodeList[idx];
10141016
}
10151017
} else {
10161018
stableNodeList = nodeList;
10171019
}
10181020

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];
1021+
for(i = 0, ii = linkFns.length; i < ii; ) {
1022+
node = stableNodeList[ linkFns[i++] ]
10221023
nodeLinkFn = linkFns[i++];
10231024
childLinkFn = linkFns[i++];
10241025

0 commit comments

Comments
 (0)