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

Commit 89268af

Browse files
committed
perf(ngClass): only access the element's data once
1 parent 0ba4be6 commit 89268af

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/ng/directive/ngClass.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ function classDirective(name, selector) {
1313
return {
1414
restrict: 'AC',
1515
link: function(scope, element, attr) {
16+
var classCounts = element.data('$classCounts');
1617
var oldVal;
1718

19+
if (!classCounts) {
20+
// Use createMap() to prevent class assumptions involving property
21+
// names in Object.prototype
22+
classCounts = createMap();
23+
element.data('$classCounts', classCounts);
24+
}
25+
1826
if (name !== 'ngClass') {
1927
scope.$watch('$index', function($index, old$index) {
2028
/* eslint-disable no-bitwise */
@@ -44,10 +52,8 @@ function classDirective(name, selector) {
4452
}
4553

4654
function digestClassCounts(classes, count) {
47-
// Use createMap() to prevent class assumptions involving property
48-
// names in Object.prototype
49-
var classCounts = element.data('$classCounts') || createMap();
5055
var classesToUpdate = [];
56+
5157
forEach(classes, function(className) {
5258
if (count > 0 || classCounts[className]) {
5359
classCounts[className] = (classCounts[className] || 0) + count;
@@ -56,7 +62,7 @@ function classDirective(name, selector) {
5662
}
5763
}
5864
});
59-
element.data('$classCounts', classCounts);
65+
6066
return classesToUpdate.join(' ');
6167
}
6268

0 commit comments

Comments
 (0)