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

Commit 8643b6a

Browse files
committed
perf(ngClass): optimize the case of static map of classes with large objects
1 parent c75fb80 commit 8643b6a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/ng/directive/ngClass.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
'use strict';
22

33
function classDirective(name, selector) {
4+
var staticMapClassRegEx = /^\s*\{/;
45
name = 'ngClass' + name;
56
return ['$animate', function($animate) {
67
return {
78
restrict: 'AC',
89
link: function(scope, element, attr) {
910
var oldVal;
1011

11-
scope.$watch(attr[name], ngClassWatchAction, true);
12+
// shortcut: if it is clearly a map of classes do not copy values, they are supposed to be boolean (truly/falsy)
13+
scope[staticMapClassRegEx.test(attr[name]) ? '$watchCollection' : '$watch'](attr[name], ngClassWatchAction, true);
1214

1315
attr.$observe('class', function(value) {
1416
ngClassWatchAction(scope.$eval(attr[name]));

test/ng/directive/ngClassSpec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,15 @@ describe('ngClass', function() {
409409
expect(e2.hasClass('even')).toBeTruthy();
410410
expect(e2.hasClass('odd')).toBeFalsy();
411411
}));
412+
413+
it('should not copy large objects via hard map of classes', inject(function($rootScope, $compile) {
414+
element = $compile('<div ng-class="{foo: verylargeobject}"></div>')($rootScope);
415+
$rootScope.verylargeobject = {very: 'largeobject'};
416+
$rootScope.$digest();
417+
418+
expect($rootScope.$$watchers[0].last).toEqual(jasmine.any(Number));
419+
}));
420+
412421
});
413422

414423
describe('ngClass animations', function() {

0 commit comments

Comments
 (0)