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

Commit 5b52286

Browse files
jbedardpetebacondarwin
authored andcommitted
perf($compile): replace forEach(controller) with plain loops
Closes #11084
1 parent aaae3cc commit 5b52286

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

src/ng/compile.js

+38-32
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
16711671

16721672
if (!directive.templateUrl && directive.controller) {
16731673
directiveValue = directive.controller;
1674-
controllerDirectives = controllerDirectives || {};
1674+
controllerDirectives = controllerDirectives || createMap();
16751675
assertNoDuplicate("'" + directiveName + "' controller",
16761676
controllerDirectives[directiveName], directive, $compileNode);
16771677
controllerDirectives[directiveName] = directive;
@@ -1877,6 +1877,36 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
18771877
return value || null;
18781878
}
18791879

1880+
function setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope) {
1881+
var elementControllers = createMap();
1882+
for (var controllerKey in controllerDirectives) {
1883+
var directive = controllerDirectives[controllerKey];
1884+
var locals = {
1885+
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
1886+
$element: $element,
1887+
$attrs: attrs,
1888+
$transclude: transcludeFn
1889+
};
1890+
1891+
var controller = directive.controller;
1892+
if (controller == '@') {
1893+
controller = attrs[directive.name];
1894+
}
1895+
1896+
var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
1897+
1898+
// For directives with element transclusion the element is a comment,
1899+
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1900+
// clean up (http://bugs.jquery.com/ticket/8335).
1901+
// Instead, we save the controllers for the element in a local hash and attach to .data
1902+
// later, once we have the actual element.
1903+
elementControllers[directive.name] = controllerInstance;
1904+
if (!hasElementTranscludeDirective) {
1905+
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
1906+
}
1907+
}
1908+
return elementControllers;
1909+
}
18801910

18811911
function nodeLinkFn(childLinkFn, scope, linkNode, $rootElement, boundTranscludeFn,
18821912
thisLinkFn) {
@@ -1903,32 +1933,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19031933
}
19041934

19051935
if (controllerDirectives) {
1906-
elementControllers = {};
1907-
forEach(controllerDirectives, function(directive) {
1908-
var locals = {
1909-
$scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
1910-
$element: $element,
1911-
$attrs: attrs,
1912-
$transclude: transcludeFn
1913-
}, controllerInstance;
1914-
1915-
controller = directive.controller;
1916-
if (controller == '@') {
1917-
controller = attrs[directive.name];
1918-
}
1919-
1920-
controllerInstance = $controller(controller, locals, true, directive.controllerAs);
1921-
1922-
// For directives with element transclusion the element is a comment,
1923-
// but jQuery .data doesn't support attaching data to comment nodes as it's hard to
1924-
// clean up (http://bugs.jquery.com/ticket/8335).
1925-
// Instead, we save the controllers for the element in a local hash and attach to .data
1926-
// later, once we have the actual element.
1927-
elementControllers[directive.name] = controllerInstance;
1928-
if (!hasElementTranscludeDirective) {
1929-
$element.data('$' + directive.name + 'Controller', controllerInstance.instance);
1930-
}
1931-
});
1936+
elementControllers = setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope);
19321937
}
19331938

19341939
if (newIsolateScopeDirective) {
@@ -1958,17 +1963,18 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19581963
bindings, scopeDirective);
19591964
}
19601965
}
1961-
forEach(elementControllers, function(controller) {
1962-
var result = controller();
1963-
if (result !== controller.instance &&
1966+
for (i in elementControllers) {
1967+
controller = elementControllers[i];
1968+
var controllerResult = controller();
1969+
if (controllerResult !== controller.instance &&
19641970
controller === controllerForBindings) {
19651971
// Remove and re-install bindToController bindings
19661972
thisLinkFn.$$destroyBindings();
19671973
thisLinkFn.$$destroyBindings =
1968-
initializeDirectiveBindings(scope, attrs, result,
1974+
initializeDirectiveBindings(scope, attrs, controllerResult,
19691975
bindings, scopeDirective);
19701976
}
1971-
});
1977+
}
19721978
}
19731979

19741980
// PRELINKING

0 commit comments

Comments
 (0)