@@ -960,7 +960,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
960
960
function compileNodes ( nodeList , transcludeFn , $rootElement , maxPriority , ignoreDirective ,
961
961
previousCompileContext ) {
962
962
var linkFns = [ ] ,
963
- attrs , directives , nodeLinkFn , childNodes , childLinkFn , linkFnFound , nodeLinkFnFound ;
963
+ attrs , directives , nodeLinkFn , childNodes , childLinkFn , nodeLinkFnFound ;
964
964
965
965
for ( var i = 0 ; i < nodeList . length ; i ++ ) {
966
966
attrs = new Attributes ( ) ;
@@ -987,38 +987,39 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
987
987
( nodeLinkFn . transcludeOnThisElement || ! nodeLinkFn . templateOnThisElement )
988
988
&& nodeLinkFn . transclude ) : transcludeFn ) ;
989
989
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
+ }
993
994
994
995
//use the previous context only for the first element in the virtual group
995
996
previousCompileContext = null ;
996
997
}
997
998
998
999
// return a linking function if we have found anything, null otherwise
999
- return linkFnFound ? compositeLinkFn : null ;
1000
+ return linkFns . length ? compositeLinkFn : null ;
1000
1001
1001
1002
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 ;
1003
1004
var stableNodeList ;
1004
1005
1005
1006
1006
1007
if ( nodeLinkFnFound ) {
1007
1008
// copy nodeList so that if a nodeLinkFn removes or adds an element at this DOM level our
1008
1009
// offsets don't get screwed up
1009
- var nodeListLength = nodeList . length ;
1010
- stableNodeList = new Array ( nodeListLength ) ;
1010
+ stableNodeList = [ ] ;
1011
1011
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 ] ;
1014
1016
}
1015
1017
} else {
1016
1018
stableNodeList = nodeList ;
1017
1019
}
1018
1020
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 ++ ] ]
1022
1023
nodeLinkFn = linkFns [ i ++ ] ;
1023
1024
childLinkFn = linkFns [ i ++ ] ;
1024
1025
0 commit comments