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

Commit 3e0a2e1

Browse files
committed
perf($compile): clone the nodeList during linking only if necessary
1 parent 1e8698b commit 3e0a2e1

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/ng/compile.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
934934
function compileNodes(nodeList, transcludeFn, $rootElement, maxPriority, ignoreDirective,
935935
previousCompileContext) {
936936
var linkFns = [],
937-
attrs, directives, nodeLinkFn, childNodes, childLinkFn, linkFnFound;
937+
attrs, directives, nodeLinkFn, childNodes, childLinkFn, linkFnFound, nodeLinkFnFound;
938938

939939
for (var i = 0; i < nodeList.length; i++) {
940940
attrs = new Attributes();
@@ -963,6 +963,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
963963

964964
linkFns.push(nodeLinkFn, childLinkFn);
965965
linkFnFound = linkFnFound || nodeLinkFn || childLinkFn;
966+
nodeLinkFnFound = nodeLinkFnFound || nodeLinkFn;
967+
966968
//use the previous context only for the first element in the virtual group
967969
previousCompileContext = null;
968970
}
@@ -972,14 +974,23 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
972974

973975
function compositeLinkFn(scope, nodeList, $rootElement, parentBoundTranscludeFn) {
974976
var nodeLinkFn, childLinkFn, node, childScope, i, ii, n, childBoundTranscludeFn;
977+
var stableNodeList;
978+
979+
980+
if (nodeLinkFnFound) {
981+
// copy nodeList so that if a nodeLinkFn removes or adds an element at this DOM level our
982+
// offsets don't get screwed up
983+
var nodeListLength = nodeList.length;
984+
stableNodeList = new Array(nodeListLength);
975985

976-
// copy nodeList so that linking doesn't break due to live list updates.
977-
var nodeListLength = nodeList.length,
978-
stableNodeList = new Array(nodeListLength);
979-
for (i = 0; i < nodeListLength; i++) {
980-
stableNodeList[i] = nodeList[i];
986+
for (i = 0; i < nodeListLength; i++) {
987+
stableNodeList[i] = nodeList[i];
988+
}
989+
} else {
990+
stableNodeList = nodeList;
981991
}
982992

993+
// TODO(perf): when the DOM is sparsely annotated with directives, we spend a lot of time iterating over nulls here
983994
for(i = 0, n = 0, ii = linkFns.length; i < ii; n++) {
984995
node = stableNodeList[n];
985996
nodeLinkFn = linkFns[i++];

0 commit comments

Comments
 (0)