[lexical] perf: defer DOM Selection property reads in $updateDOMSelection#8422
Merged
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Accessing `domSelection.anchorNode`, `.focusNode`, `.anchorOffset`, and
`.focusOffset` forces the browser to resolve the selection against the
current layout, triggering synchronous style/layout recalculation.
Previously these four properties were read eagerly at the top of
$updateDOMSelection, even when the function would immediately return
via one of two early-exit paths (collaboration tag check, or
`!isRangeSelection(nextSelection)`).
During initial mount, both `prevSelection` and `nextSelection` are null,
so the function hits the `!isRangeSelection` branch and returns — but
only after the browser has already paid the layout recalc cost for
property reads that are never consumed.
This commit:
- Keeps `document.activeElement` at the top (needed for the first
early return)
- Inlines `domSelection.anchorNode`/`.focusNode` in the
`isSelectionWithinEditor` call (only reached when
`prevSelection !== null`)
- Defers the four property reads to just before the diff-check block
that actually uses them
<!-- TODO: Replace with performance trace screenshot showing the
style/layout recalc cost before this fix -->
The net effect is zero layout-triggering Selection reads on mount and
on any code path that exits early.
902d3a6 to
91c39ac
Compare
etrepum
approved these changes
Apr 30, 2026
Contributor
|
It will be interesting to look at the performance trace after these changes |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Accessing
domSelection.anchorNode,.focusNode,.anchorOffset, and.focusOffsetforces the browser to resolve the selection against the current layout, triggering synchronous style/layout recalculation.Previously these four properties were read eagerly at the top of
$updateDOMSelection, even when the function would immediately return via one of two early-exit paths:!isRangeSelection(nextSelection)(the common case on mount)During initial mount, both
prevSelectionandnextSelectionare null, so the function hits the!isRangeSelectionbranch and returns — but only after the browser has already paid the layout recalc cost for property reads that are never consumed.Performance trace
Changes
document.activeElementat the top (needed for the first early return)domSelection.anchorNode/.focusNodedirectly in theisSelectionWithinEditorcall (only reached whenprevSelection !== null)anchorNode,focusNode,anchorOffset,focusOffset) to just after the early returns, right before the diff-check block that actually uses themImpact
Zero layout-triggering
Selectionreads on mount and on any code path that exits early. This eliminates unnecessary forced style/layout recalculations during Lexical node mounting.