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

Commit ce5ffbf

Browse files
committed
perf(animate): avoid unnecessary computations if animations are globally disabled
Closes #14914
1 parent ab114af commit ce5ffbf

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

src/ngAnimate/animateQueue.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,11 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
348348
options.to = null;
349349
}
350350

351-
// there are situations where a directive issues an animation for
352-
// a jqLite wrapper that contains only comment nodes... If this
353-
// happens then there is no way we can perform an animation
354-
if (!node ||
351+
// If animations are hard-disabled for the whole application there is no need to continue.
352+
// There are also situations where a directive issues an animation for a jqLite wrapper that
353+
// contains only comment nodes. In this case, there is no way we can perform an animation.
354+
if (!animationsEnabled ||
355+
!node ||
355356
!isAnimatableByFilter(node, event, initialOptions) ||
356357
!isAnimatableClassName(node, options)) {
357358
close();
@@ -362,12 +363,11 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
362363

363364
var documentHidden = $$isDocumentHidden();
364365

365-
// this is a hard disable of all animations for the application or on
366-
// the element itself, therefore there is no need to continue further
367-
// past this point if not enabled
366+
// This is a hard disable of all animations the element itself, therefore there is no need to
367+
// continue further past this point if not enabled
368368
// Animations are also disabled if the document is currently hidden (page is not visible
369369
// to the user), because browsers slow down or do not flush calls to requestAnimationFrame
370-
var skipAnimations = !animationsEnabled || documentHidden || disabledElementsLookup.get(node);
370+
var skipAnimations = documentHidden || disabledElementsLookup.get(node);
371371
var existingAnimation = (!skipAnimations && activeAnimationsLookup.get(node)) || {};
372372
var hasExistingAnimation = !!existingAnimation.state;
373373

test/ngAnimate/animateSpec.js

+49
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,32 @@ describe('animations', function() {
307307
});
308308
});
309309

310+
it('should not try to match the `classNameFilter` RegExp if animations are globally disabled',
311+
function() {
312+
var regex = /foo/;
313+
var regexTestSpy = spyOn(regex, 'test').and.callThrough();
314+
315+
module(function($animateProvider) {
316+
$animateProvider.classNameFilter(regex);
317+
});
318+
319+
inject(function($animate) {
320+
$animate.addClass(element, 'foo');
321+
expect(regexTestSpy).toHaveBeenCalled();
322+
323+
regexTestSpy.calls.reset();
324+
$animate.enabled(false);
325+
$animate.addClass(element, 'bar');
326+
expect(regexTestSpy).not.toHaveBeenCalled();
327+
328+
regexTestSpy.calls.reset();
329+
$animate.enabled(true);
330+
$animate.addClass(element, 'baz');
331+
expect(regexTestSpy).toHaveBeenCalled();
332+
});
333+
}
334+
);
335+
310336
describe('customFilter()', function() {
311337
it('should be `null` by default', module(function($animateProvider) {
312338
expect($animateProvider.customFilter()).toBeNull();
@@ -434,6 +460,29 @@ describe('animations', function() {
434460
expect(element.parent()[0]).toBeUndefined();
435461
});
436462
});
463+
464+
it('should not execute the function if animations are globally disabled', function() {
465+
var customFilterSpy = jasmine.createSpy('customFilterFn');
466+
467+
module(function($animateProvider) {
468+
$animateProvider.customFilter(customFilterSpy);
469+
});
470+
471+
inject(function($animate) {
472+
$animate.addClass(element, 'foo');
473+
expect(customFilterSpy).toHaveBeenCalled();
474+
475+
customFilterSpy.calls.reset();
476+
$animate.enabled(false);
477+
$animate.addClass(element, 'bar');
478+
expect(customFilterSpy).not.toHaveBeenCalled();
479+
480+
customFilterSpy.calls.reset();
481+
$animate.enabled(true);
482+
$animate.addClass(element, 'baz');
483+
expect(customFilterSpy).toHaveBeenCalled();
484+
});
485+
});
437486
});
438487

439488
describe('enabled()', function() {

0 commit comments

Comments
 (0)