Skip to content

Commit 64cf19c

Browse files
authored
perf(material/tooltip): Avoid unneeded calls to clearTimeout (#29643)
1 parent 3bf0e31 commit 64cf19c

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/material/tooltip/tooltip.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
354354
private _document: Document;
355355

356356
/** Timer started at the last `touchstart` event. */
357-
private _touchstartTimeout: ReturnType<typeof setTimeout>;
357+
private _touchstartTimeout: null | ReturnType<typeof setTimeout> = null;
358358

359359
/** Emits when the component is destroyed. */
360360
private readonly _destroyed = new Subject<void>();
@@ -434,7 +434,10 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
434434
ngOnDestroy() {
435435
const nativeElement = this._elementRef.nativeElement;
436436

437-
clearTimeout(this._touchstartTimeout);
437+
// Optimization: Do not call clearTimeout unless there is an active timer.
438+
if (this._touchstartTimeout) {
439+
clearTimeout(this._touchstartTimeout);
440+
}
438441

439442
if (this._overlayRef) {
440443
this._overlayRef.dispose();
@@ -802,13 +805,15 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
802805
// Note that it's important that we don't `preventDefault` here,
803806
// because it can prevent click events from firing on the element.
804807
this._setupPointerExitEventsIfNeeded();
805-
clearTimeout(this._touchstartTimeout);
808+
if (this._touchstartTimeout) {
809+
clearTimeout(this._touchstartTimeout);
810+
}
806811

807812
const DEFAULT_LONGPRESS_DELAY = 500;
808-
this._touchstartTimeout = setTimeout(
809-
() => this.show(undefined, origin),
810-
this._defaultOptions.touchLongPressShowDelay ?? DEFAULT_LONGPRESS_DELAY,
811-
);
813+
this._touchstartTimeout = setTimeout(() => {
814+
this._touchstartTimeout = null;
815+
this.show(undefined, origin);
816+
}, this._defaultOptions.touchLongPressShowDelay ?? DEFAULT_LONGPRESS_DELAY);
812817
},
813818
]);
814819
}
@@ -839,7 +844,9 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
839844
} else if (this.touchGestures !== 'off') {
840845
this._disableNativeGesturesIfNecessary();
841846
const touchendListener = () => {
842-
clearTimeout(this._touchstartTimeout);
847+
if (this._touchstartTimeout) {
848+
clearTimeout(this._touchstartTimeout);
849+
}
843850
this.hide(this._defaultOptions.touchendHideDelay);
844851
};
845852

0 commit comments

Comments
 (0)