From 60e22d297e6d6e21315bcdf9cec015f6b03d3bef Mon Sep 17 00:00:00 2001 From: Karl Seamon Date: Mon, 26 Aug 2024 14:54:54 -0400 Subject: [PATCH] perf(material/tooltip): Avoid unneeded calls to clearTimeout --- src/material/tooltip/tooltip.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/material/tooltip/tooltip.ts b/src/material/tooltip/tooltip.ts index 71a5409d5339..1af416d83817 100644 --- a/src/material/tooltip/tooltip.ts +++ b/src/material/tooltip/tooltip.ts @@ -354,7 +354,7 @@ export class MatTooltip implements OnDestroy, AfterViewInit { private _document: Document; /** Timer started at the last `touchstart` event. */ - private _touchstartTimeout: ReturnType; + private _touchstartTimeout: null | ReturnType = null; /** Emits when the component is destroyed. */ private readonly _destroyed = new Subject(); @@ -434,7 +434,10 @@ export class MatTooltip implements OnDestroy, AfterViewInit { ngOnDestroy() { const nativeElement = this._elementRef.nativeElement; - clearTimeout(this._touchstartTimeout); + // Optimization: Do not call clearTimeout unless there is an active timer. + if (this._touchstartTimeout) { + clearTimeout(this._touchstartTimeout); + } if (this._overlayRef) { this._overlayRef.dispose(); @@ -802,13 +805,15 @@ export class MatTooltip implements OnDestroy, AfterViewInit { // Note that it's important that we don't `preventDefault` here, // because it can prevent click events from firing on the element. this._setupPointerExitEventsIfNeeded(); - clearTimeout(this._touchstartTimeout); + if (this._touchstartTimeout) { + clearTimeout(this._touchstartTimeout); + } const DEFAULT_LONGPRESS_DELAY = 500; - this._touchstartTimeout = setTimeout( - () => this.show(undefined, origin), - this._defaultOptions.touchLongPressShowDelay ?? DEFAULT_LONGPRESS_DELAY, - ); + this._touchstartTimeout = setTimeout(() => { + this._touchstartTimeout = null; + this.show(undefined, origin); + }, this._defaultOptions.touchLongPressShowDelay ?? DEFAULT_LONGPRESS_DELAY); }, ]); } @@ -839,7 +844,9 @@ export class MatTooltip implements OnDestroy, AfterViewInit { } else if (this.touchGestures !== 'off') { this._disableNativeGesturesIfNecessary(); const touchendListener = () => { - clearTimeout(this._touchstartTimeout); + if (this._touchstartTimeout) { + clearTimeout(this._touchstartTimeout); + } this.hide(this._defaultOptions.touchendHideDelay); };