Use memoization to avoid unnecessary textContent
updates in TextString
component
#5306
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
This PR changes the approach used in the
<TextString>
component to synchronize the DOM and the Virtual DOM.Previously, the initial render of the
<TextString>
component would render the initial text, but the second render would remove the text from the DOM, before quickly being re-committed to the DOM in the layout effect before the browser had time to paint.This PR takes a different approach that avoids ever having to render an empty
textContent
on the second render. Instead, we render a memoized component in<TextString>
(<MemoizedText>
), that receives props that are referentially stable across re-renders, such that React never updates the DOM across re-renders.This PR provides a significant performance improvement over the current implementation, as previously the first update to an element would cause all its text leaves to re-render and wastefully update their
textContent
even if it had not changed.Issue
I couldn't find any related tickets.
Example
Before
Screen.Recording.2023-02-22.at.5.41.31.PM.mov
After
Screen.Recording.2023-02-22.at.5.40.35.PM.mov
Checks
yarn test
.yarn lint
. (Fix errors withyarn fix
.)yarn start
.)yarn changeset add
.)