feat: Tooltip - Performance - avoid calling clearTimeout on unmount if timeout id is 0 #3163
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Context:
We have a product list page with products with multiple color variants. These color variants have the Tooltip component to display the color name on hover.
Issue:
We have noticed a significant amount of
clearTimeout
calls when the component with the Tooltips unmounts. That's interesting because it happens even when we don't trigger any of the Tooltips in the page.After some investigation, we noticed this is caused by a
window.clearTimeout
that's happening in the component unmount.Fix:
The fix proposed is quite simple. I'm checking
openTimerRef.current
and only calling this if it's not0
. I'm also resettingopenTimerRef.current
to0
after eachclearTimeout()
. We have noticed a reduction in the number ofclearTimeout
calls in the unmount of this component. This means a significant performance improvement in our scenario given we have dozens of Tooltips in some of our pages.