Repository navigation
[lexical] Bug Fix: Compute the document's text on commit only when a text content listener is registered - #9548
Merged
Merged
Conversation
…text content listener is registered ## Description Every commit calls `triggerTextContentListeners`, which computes the text of the previous and the next editor state to compare them, also when no text content listener is registered. `RootNode.__cachedText` is set only by the reconciler, so in a headless editor each computation walks the whole document: a one-character edit of a 10,000-paragraph headless editor computed the root's text twice (40 calls for 20 edits) at about 30 ms per edit. That cost falls on servers, scripts, tests and headless collaboration (`@lexical/y`'s `editYState`/`readYState` and a headless relay editor). `triggerTextContentListeners` now returns before computing anything when `editor._listeners.textcontent` is empty. With a listener nothing changes: it is called with the new text when the text changed, as before. Closes #9518 ## Changeset `.changeset/lexical-text-content-without-listeners.md` (`lexical`, `[perf]`). ## Test plan New `HeadlessTextContent.test.ts`: no computation of the root's text while nothing listens, and a listener still gets each change of the text (and not a format-only change), with nothing computed again once it is unregistered. ``` pnpm exec vitest run --project unit --reporter=verbose packages/lexical/src/__tests__/unit/HeadlessTextContent.test.ts ``` ### Before ``` × |unit| packages/lexical/src/__tests__/unit/HeadlessTextContent.test.ts > text content listeners in a headless editor > a commit computes no document text while nothing listens Received: × |unit| packages/lexical/src/__tests__/unit/HeadlessTextContent.test.ts > text content listeners in a headless editor > a listener still gets each change of the text, and only those Received: ⎯⎯⎯⎯⎯⎯⎯ Failed Tests 2 ⎯⎯⎯⎯⎯⎯⎯ AssertionError: expected "getTextContent" to not be called at all, but actually been called 40 times Received: AssertionError: expected "getTextContent" to not be called at all, but actually been called 2 times Received: Test Files 1 failed (1) Tests 2 failed (2) ``` ### After ``` ✓ |unit| packages/lexical/src/__tests__/unit/HeadlessTextContent.test.ts > text content listeners in a headless editor > a commit computes no document text while nothing listens ✓ |unit| packages/lexical/src/__tests__/unit/HeadlessTextContent.test.ts > text content listeners in a headless editor > a listener still gets each change of the text, and only those Test Files 1 passed (1) Tests 2 passed (2) ``` The issue's repro (10,000 paragraphs, 20 edits): before `root text computed 40 times for 20 edits, 31.5 ms per edit`, after `root text computed 0 times for 20 edits, 3.6 ms per edit`. Full unit suite (`pnpm exec vitest run --project unit`), `lexical` core included: 568 files, 14,657 passed, 46 expected fail, 0 failed. `pnpm run ci-check` passes.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
zurfyx
requested review from
acywatson,
etrepum,
ivailop7 and
potatowagon
as code owners
October 29, 2026 23:16
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
## Description This commit changes no file. The repository's review bot reviews a pull request when its branch is pushed; this branch was last pushed before the bot started reviewing the repository, so it had no review yet.
There was a problem hiding this comment.
This looks good to me.
The early return in triggerTextContentListeners (packages/lexical/src/LexicalUpdates.ts:888) skips only the text computation whose result fed the listener dispatch. Editors that register a text content listener behave as before, and headless editors stop walking the whole document on every commit.
What I confirmed:
- The check runs at the same point in the commit as the old computation, after mutation and decorator listeners and before update listeners, so a text content listener that one of them registers during a commit is still notified in that commit.
HeadlessTextContent.test.tsfails againstmain(the root text is computed 40 times for 20 edits, and twice after unregistering) and passes with this change. Its second test also pins the listener's payload and the format-only no-op, which guards the path with a listener.- The changeset matches
.changeset/README.md('lexical': patch,[perf]). It needs noVerify:paragraph, since output and notifications are unchanged.
No changes requested.
Merged
This branch was successfully deployed
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.
Description
Every commit calls
triggerTextContentListeners, which computes the text of the previous and the next editor state to compare them, also when no text content listener is registered.RootNode.__cachedTextis set only by the reconciler, so in a headless editor each computation walks the whole document: a one-character edit of a 10,000-paragraph headless editor computed the root's text twice (40 calls for 20 edits) at about 30 ms per edit. That cost falls on servers, scripts, tests and headless collaboration (@lexical/y'seditYState/readYStateand a headless relay editor).triggerTextContentListenersnow returns before computing anything wheneditor._listeners.textcontentis empty. With a listener nothing changes: it is called with the new text when the text changed, as before.Closes #9518
Changeset
.changeset/lexical-text-content-without-listeners.md(lexical,[perf]).Test plan
New
HeadlessTextContent.test.ts: no computation of the root's text while nothing listens, and a listener still gets each change of the text (and not a format-only change), with nothing computed again once it is unregistered.Before
After
The issue's repro (10,000 paragraphs, 20 edits): before
root text computed 40 times for 20 edits, 31.5 ms per edit, afterroot text computed 0 times for 20 edits, 3.6 ms per edit. Full unit suite (pnpm exec vitest run --project unit),lexicalcore included: 568 files, 14,657 passed, 46 expected fail, 0 failed.pnpm run ci-checkpasses.