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

Commit f3a796e

Browse files
kseamonIgorMinar
authored andcommitted
perf(compile): add class 'ng-scope' before cloning and other micro-optimizations
Add class ng-scope to dom nodes during directive compile rather than link. Optimize handling of nodeLists. This results in a savings of about 130ms during the startup of a product within Google. Closes #5471
1 parent 09f8962 commit f3a796e

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/ng/compile.js

+20-15
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
818818
var compositeLinkFn =
819819
compileNodes($compileNodes, transcludeFn, $compileNodes,
820820
maxPriority, ignoreDirective, previousCompileContext);
821+
safeAddClass($compileNodes, 'ng-scope');
821822
return function publicLinkFn(scope, cloneConnectFn, transcludeControllers){
822823
assertArg(scope, 'scope');
823824
// important!!: we must call our jqLite.clone() since the jQuery one is trying to be smart
@@ -832,12 +833,13 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
832833

833834
// Attach scope only to non-text nodes.
834835
for(var i = 0, ii = $linkNode.length; i<ii; i++) {
835-
var node = $linkNode[i];
836-
if (node.nodeType == 1 /* element */ || node.nodeType == 9 /* document */) {
836+
var node = $linkNode[i],
837+
nodeType = node.nodeType;
838+
if (nodeType === 1 /* element */ || nodeType === 9 /* document */) {
837839
$linkNode.eq(i).data('$scope', scope);
838840
}
839841
}
840-
safeAddClass($linkNode, 'ng-scope');
842+
841843
if (cloneConnectFn) cloneConnectFn($linkNode, scope);
842844
if (compositeLinkFn) compositeLinkFn(scope, $linkNode, $linkNode);
843845
return $linkNode;
@@ -871,9 +873,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
871873
function compileNodes(nodeList, transcludeFn, $rootElement, maxPriority, ignoreDirective,
872874
previousCompileContext) {
873875
var linkFns = [],
874-
nodeLinkFn, childLinkFn, directives, attrs, linkFnFound;
876+
attrs, directives, nodeLinkFn, childNodes, childLinkFn, linkFnFound;
875877

876-
for(var i = 0; i < nodeList.length; i++) {
878+
for (var i = 0; i < nodeList.length; i++) {
877879
attrs = new Attributes();
878880

879881
// we must always refer to nodeList[i] since the nodes can be replaced underneath us.
@@ -885,16 +887,19 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
885887
null, [], [], previousCompileContext)
886888
: null;
887889

890+
if (nodeLinkFn && nodeLinkFn.scope) {
891+
safeAddClass(jqLite(nodeList[i]), 'ng-scope');
892+
}
893+
888894
childLinkFn = (nodeLinkFn && nodeLinkFn.terminal ||
889-
!nodeList[i].childNodes ||
890-
!nodeList[i].childNodes.length)
895+
!(childNodes = nodeList[i].childNodes) ||
896+
!childNodes.length)
891897
? null
892-
: compileNodes(nodeList[i].childNodes,
898+
: compileNodes(childNodes,
893899
nodeLinkFn ? nodeLinkFn.transclude : transcludeFn);
894900

895-
linkFns.push(nodeLinkFn);
896-
linkFns.push(childLinkFn);
897-
linkFnFound = (linkFnFound || nodeLinkFn || childLinkFn);
901+
linkFns.push(nodeLinkFn, childLinkFn);
902+
linkFnFound = linkFnFound || nodeLinkFn || childLinkFn;
898903
//use the previous context only for the first element in the virtual group
899904
previousCompileContext = null;
900905
}
@@ -906,9 +911,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
906911
var nodeLinkFn, childLinkFn, node, $node, childScope, childTranscludeFn, i, ii, n;
907912

908913
// copy nodeList so that linking doesn't break due to live list updates.
909-
var stableNodeList = [];
910-
for (i = 0, ii = nodeList.length; i < ii; i++) {
911-
stableNodeList.push(nodeList[i]);
914+
var nodeListLength = nodeList.length,
915+
stableNodeList = new Array(nodeListLength);
916+
for (i = 0; i < nodeListLength; i++) {
917+
stableNodeList[i] = nodeList[i];
912918
}
913919

914920
for(i = 0, n = 0, ii = linkFns.length; i < ii; n++) {
@@ -921,7 +927,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
921927
if (nodeLinkFn.scope) {
922928
childScope = scope.$new();
923929
$node.data('$scope', childScope);
924-
safeAddClass($node, 'ng-scope');
925930
} else {
926931
childScope = scope;
927932
}

0 commit comments

Comments
 (0)