Skip to content

[lexical] Bug Fix: Compute the document's text on commit only when a text content listener is registered - #9548

Merged
zurfyx merged 2 commits into
mainfrom
zurfyx/fix-9518-text-content-listeners
Oct 30, 2026
Merged

zurfyx merged 2 commits into
mainfrom
zurfyx/fix-9518-text-content-listeners

Conversation

@zurfyx

@zurfyx zurfyx commented Oct 29, 2026

Copy link
Copy Markdown
Member

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.

…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.
@vercel

vercel Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Oct 30, 2026 5:43pm UTC
lexical-playground Ready Ready Preview Oct 30, 2026 5:43pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 29, 2026
@pkg-pr-new

pkg-pr-new Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9548

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9548

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9548

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9548

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9548

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9548

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9548

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9548

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9548

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9548

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9548

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9548

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9548

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9548

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9548

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9548

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9548

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9548

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9548

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9548

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9548

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9548

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9548

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9548

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9548

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9548

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9548

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9548

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9548

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9548

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9548

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9548

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9548

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9548

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9548

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9548

commit: 02d9159

## 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.

@meta-code-review meta-code-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.ts fails against main (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 no Verify: paragraph, since output and notifications are unchanged.

No changes requested.

@zurfyx
zurfyx added this pull request to the merge queue Oct 30, 2026
Merged via the queue into main with commit f40f54a Oct 30, 2026
57 checks passed
@potatowagon potatowagon mentioned this pull request Dec 2, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 02d9159d Deployed Oct 30, 2026 by vercel[bot]
Preview – lexical-playground — 02d9159d Deployed Oct 30, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: [lexical] Every commit of a headless editor computes the whole document's text twice, even with no text content listener

1 participant