Skip to content

[lexical] Perf: Skip rewriting an unchanged contentEditable on slot containers - #9743

Merged
emilyhorsman merged 2 commits into
mainfrom
emilyhorsman/skip-unchanged-contenteditable
Nov 3, 2026
Merged

emilyhorsman merged 2 commits into
mainfrom
emilyhorsman/skip-unchanged-contenteditable

Conversation

@emilyhorsman

@emilyhorsman emilyhorsman commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

$applySlotEditable runs on every mount and every reconcile of a slot host. For a slot container inside a non-editable host (for example a decorator host), it calls $markSlotEditable, which assigned element.contentEditable unconditionally, even when the container already had that value.

That write is not free. In Chrome, any write of contenteditable to a connected element forces a synchronous style recalculation, even when the value is unchanged (writing another attribute does not). So every reconcile that touches a slot host after other DOM work pays a forced style pass in the middle of the commit, and later writes in the same commit dirty style again.

$markSlotEditable now writes contentEditable only when the contenteditable attribute differs from the target value. It compares with getAttribute('contenteditable') rather than the contentEditable getter, because the getter returns 'inherit' when the attribute is absent and would never match. An editable toggle still reaches every container (setEditable re-applies through the same function, and the value changes then). The DOM ends in the same state for every caller.

Measurement

On a page with a 3,014-block document (Chrome, 1440x900), after dirtying one style:

write on a connected slot container time
el.contentEditable = sameValue 93–132 ms
native el.setAttribute('contenteditable', sameValue) 91–117 ms
native el.setAttribute('data-x', ...) 0–0.1 ms
reading el.contentEditable / el.isContentEditable 0 ms

With this patch applied to that app's first reconcile after load (4 slot containers, 12 $markSlotEditable calls; median of 5 alternating loads per arm), time inside $markSlotEditable dropped from 64 ms to 7 ms and the editor build frame shrank by 12–29 ms. The frame saves less than the full 57 ms because most of the forced style work is the document's first style pass, which the next layout read or paint pays anyway; the patch removes the repeated part.

The review on this PR measured the same thing in headless Chromium on a 3,000-paragraph page: with style dirty across the document, the write costs 9.1 ms per container without the guard and 0 ms with it; with only layout dirty it costs 0.1 ms, which is why this says "style recalculation" rather than "style and layout" (second commit).

Repro (any page with a large DOM)

const frame = () => new Promise(r => requestAnimationFrame(() => setTimeout(r)));
const el = document.querySelector('[contenteditable]'); // any connected element
await frame();
document.body.style.setProperty('--probe', Math.random()); // dirty style
let t = performance.now();
el.setAttribute('contenteditable', el.getAttribute('contenteditable'));
console.log('same-value contenteditable write', performance.now() - t, 'ms');

Changeset

.changeset/lexical-slot-contenteditable-unchanged.md (lexical patch, [perf]; no Verify: paragraph, since the DOM ends in the same state).

Test plan

New LexicalSlot.test.ts case, re-reconciling a decorator-host slot leaves an unchanged contentEditable alone (lexical). It wraps the contentEditable setter and counts writes on slot containers:

  • mounting a decorator host with a slot writes 'true' once;
  • reconciling the host and its slot again (the host marked dirty, the slot's text changed) writes nothing, and the container is the same element with the new text;
  • setEditable(false) writes 'false' once, so a toggle still reaches the container.
$ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts -t 'leaves an unchanged contentEditable alone'

Before

main (2b388b9), with the new test:

 × re-reconciling a decorator-host slot leaves an unchanged contentEditable alone 149ms
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
AssertionError: expected [ 'true', 'true' ] to deeply equal [ 'true' ]
 ❯ packages/lexical/src/__tests__/unit/LexicalSlot.test.ts:1177:22
    1177|       expect(writes).toEqual(['true']);
 Test Files  1 failed (1)
      Tests  1 failed | 124 skipped (125)

After

$ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts -t 'leaves an unchanged contentEditable alone'
 Test Files  1 passed (1)
      Tests  1 passed | 124 skipped (125)
$ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts packages/lexical/src/__tests__/unit/LexicalDOMSlot.test.tsx packages/lexical/src/__tests__/unit/LexicalSlotSelection.test.ts packages/lexical/src/__tests__/unit/LexicalEditorListener.test.ts
 Test Files  4 passed (4)
      Tests  200 passed (200)

All unit tests (vitest --run --project unit): 29,149 passed, 373 expected fail, 136 skipped, 0 failed. The 4 lexical-devtools test files did not load locally because their generated .wxt/tsconfig.json is missing (WXT's prepare step crashes in the environment I ran this in); they do not touch lexical, and CI's core-tests / unit job runs them.

The ci-check steps pass: tsc, tsc-test, tsc-scripts, tsc-website, flow, prettier, lint and check-changesets. tsc-extension needs the same WXT-generated types and could not run locally.

Only jsdom was exercised locally. The Chrome cost is measured in real Chrome (above). There is no browser-mode or e2e test for it: vitest browser mode cannot observe a forced style pass deterministically, and the behavior this changes (which writes happen) is covered at the unit level.

Related (not changed here)

  • $applySlotEditable's other branch removes contenteditable from a container in an editable host. Removing an absent attribute is already a no-op in the DOM, so it needs no guard.
  • The two other contentEditable = 'false' writes in core (a decorator's createDOM in the reconciler, and the block cursor element) set it on an element that was just created and is not connected yet, so they force no style pass.

…ontainers

## Description

`$applySlotEditable` runs on every mount and every reconcile of a slot host. For a slot container inside a non-editable host (for example a decorator host), it calls `$markSlotEditable`, which currently assigns `element.contentEditable` unconditionally, even when the container already has that value.

That write is not free. In Chrome, **any write of `contenteditable` to a connected element forces a synchronous style and layout update, even when the value is unchanged** (writing another attribute does not). So every reconcile that touches a slot host after other DOM work pays a forced style and layout of the whole document in the middle of the commit, and later writes in the same commit dirty it again.

This change makes `$markSlotEditable` write `contentEditable` only when the `contenteditable` attribute differs from the target value. It compares with `getAttribute('contenteditable')` rather than the `contentEditable` getter, because the getter returns `'inherit'` when the attribute is absent and would never match. An editable toggle still reaches every container, since the value does change then.

### Measurement

On a page with a 3,014-block document (Chrome, 1440x900), after dirtying one style:

| write on a connected slot container | time |
| --- | --- |
| `el.contentEditable = sameValue` | 93–132 ms |
| native `el.setAttribute('contenteditable', sameValue)` | 91–117 ms |
| native `el.setAttribute('data-x', ...)` | 0–0.1 ms |
| reading `el.contentEditable` / `el.isContentEditable` | 0 ms |

With this patch applied to that app's first reconcile after load (4 slot containers, 12 `$markSlotEditable` calls; median of 5 alternating loads per arm), time inside `$markSlotEditable` dropped from 64 ms to 7 ms and the editor build frame shrank by 12–29 ms. The frame saves less than the full 57 ms because most of the forced style work is the document's first style pass, which the next layout read or paint pays anyway; the patch removes the repeated part.

### Repro (any page with a large DOM)

```js
const frame = () => new Promise(r => requestAnimationFrame(() => setTimeout(r)));
const el = document.querySelector('[contenteditable]'); // any connected element
await frame();
document.body.style.setProperty('--probe', Math.random()); // dirty style
let t = performance.now();
el.setAttribute('contenteditable', el.getAttribute('contenteditable'));
console.log('same-value contenteditable write', performance.now() - t, 'ms');
```

## Changeset

`.changeset/lexical-slot-contenteditable-unchanged.md`: `[perf]` patch for `lexical`.

## Test plan

New unit test `re-reconciling a decorator-host slot leaves an unchanged contentEditable alone` in `LexicalSlot.test.ts` counts `contentEditable` writes on slot containers: one at mount, none when the host and its slot reconcile again, and one more after `setEditable(false)`.

```
node_modules/.bin/vitest run packages/lexical/src/__tests__/unit/LexicalSlot.test.ts -t 'leaves an unchanged contentEditable alone'
```

### Before

With the test but without the `LexicalUtils.ts` change (list of 124 skipped tests trimmed):

```
 ❯ |unit| packages/lexical/src/__tests__/unit/LexicalSlot.test.ts (125 tests | 1 failed | 124 skipped) 152ms
   ❯ named-slots: core foundation (63)
     × re-reconciling a decorator-host slot leaves an unchanged contentEditable alone 149ms

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯

 FAIL  |unit| packages/lexical/src/__tests__/unit/LexicalSlot.test.ts > named-slots: core foundation > re-reconciling a decorator-host slot leaves an unchanged contentEditable alone
AssertionError: expected [ 'true', 'true' ] to deeply equal [ 'true' ]

- Expected
+ Received

  [
    "true",
+   "true",
  ]

 ❯ packages/lexical/src/__tests__/unit/LexicalSlot.test.ts:1177:22
    1175|       );
    1176|       expect(slotContainer.textContent).toBe('After');
    1177|       expect(writes).toEqual(['true']);
       |                      ^
    1178|
    1179|       // An editable toggle still reaches the container.

 Test Files  1 failed (1)
      Tests  1 failed | 124 skipped (125)
```

### After

```
 ✓ |unit| packages/lexical/src/__tests__/unit/LexicalSlot.test.ts (125 tests | 124 skipped) 142ms

 Test Files  1 passed (1)
      Tests  1 passed | 124 skipped (125)
```

The slot, DOM slot, slot selection and editor listener suites all pass:

```
node_modules/.bin/vitest run packages/lexical/src/__tests__/unit/LexicalSlot.test.ts packages/lexical/src/__tests__/unit/LexicalDOMSlot.test.ts packages/lexical/src/__tests__/unit/LexicalSlotSelection.test.ts packages/lexical/src/__tests__/unit/LexicalEditorListener.test.ts

 Test Files  4 passed (4)
      Tests  200 passed (200)
```

`tsc --noEmit`, ESLint, Prettier and `check-changesets` are clean. Only jsdom was exercised here; the Chrome timing above was measured in a real browser.
@vercel

vercel Bot commented Nov 3, 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 Nov 3, 2026 5:47am UTC
lexical-playground Ready Ready Preview Nov 3, 2026 5:47am UTC

Request Review

@meta-code-review

meta-code-review Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 3, 2026, 6:01 AM UTC

@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 Nov 3, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: 79ab884

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

Thanks, this looks good. Skipping the no-op contentEditable write in $markSlotEditable leaves the same DOM state for every caller, and setEditable toggles still reach the slot containers through $fullReconcile. The new LexicalSlot.test.ts case fails without the LexicalUtils.ts change and passes with it. Comparing against getAttribute('contenteditable') instead of the getter is the right call, because the getter returns 'inherit' for a container with no attribute.

One optional, non-blocking suggestion:

  • Describe the cost as a style recalculation (packages/lexical/src/LexicalUtils.ts:3480-3481 and .changeset/lexical-slot-contenteditable-unchanged.md). Both say the write forces a synchronous "style and layout" update. In headless Chromium 149, on a 3,000-paragraph page with style and layout both dirty, a same-value write cost about the same as a getComputedStyle read (8.8 ms vs 8.7 ms) and well under an offsetHeight read (20.8 ms). With only layout dirty, it cost 0.1 ms. So the write seems to flush pending style work rather than layout, and its cost depends on how much style is dirty. The changeset becomes the CHANGELOG entry (CONTRIBUTING.md, "Changesets"), so wording like "forced a synchronous style recalculation" would be more accurate. The speedup itself reproduces: with style dirty across the document, the write costs 9.1 ms per container without the guard and 0 ms with it.

Comment thread packages/lexical/src/LexicalUtils.ts Outdated
Comment on lines +3480 to +3481
// In Chrome, any write of `contenteditable` to a connected element forces a
// synchronous style and layout update, even when the value is unchanged.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional nit: in headless Chromium 149, with style and layout both dirty, a same-value write here cost about the same as a getComputedStyle read (8.8 ms vs 8.7 ms) and well under an offsetHeight read (20.8 ms). That points to a forced style recalculation rather than layout. Maybe:

// In Chrome, any write of `contenteditable` to a connected element forces a
// synchronous style recalculation, even when the value is unchanged.

The same wording would fit the changeset entry.

…ced style recalculation (review item)

## Description

Follow-up to the review on this pull request. The comment in `$markSlotEditable`, the comment in its test and the changeset entry said that a same-value `contenteditable` write forces a synchronous style **and layout** update. The review measured in headless Chromium that, with style and layout both dirty, the write costs about as much as a `getComputedStyle` read (8.8 ms vs 8.7 ms) and well under an `offsetHeight` read (20.8 ms), and 0.1 ms with only layout dirty. So the write flushes pending style work, not layout. All three now say "style recalculation". No code change.

## Test plan

### After

```
$ ./node_modules/.bin/vitest --run --project unit packages/lexical/src/__tests__/unit/LexicalSlot.test.ts
 Test Files  1 passed (1)
      Tests  125 passed (125)
```

Prettier, ESLint and `check-changesets` are clean on the changed files.

@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 guard in $markSlotEditable (packages/lexical/src/LexicalUtils.ts:3484-3486) skips the write only when the contenteditable attribute already holds the target value, so the container ends in the same state for every caller, and setEditable toggles still reach it. The new LexicalSlot.test.ts case fails against the previous implementation (['true', 'true'] written) and passes with this change, and the cost described in the revised comment reproduces in Chromium: the same-value write tracks pending style work (about 18 ms on a 3,000-paragraph page with style dirty, 0.3 ms with only layout dirty), while the guarded path costs nothing.

One optional, non-blocking suggestion:

  • Changeset scope (.changeset/lexical-slot-contenteditable-unchanged.md:5): the summary names decorator hosts, but the skip lives in the exported $markSlotEditable, so it also applies to slot containers inside a contentEditable=false element shell and to apps that call it from updateDOM, as packages/lexical-website/docs/concepts/named-slots.md recommends. CONTRIBUTING.md asks for the entry to be written for someone who uses Lexical, so naming the function could help those callers, for example: [perf] $markSlotEditable no longer rewrites an unchanged contentEditable, so reconciling a slot host inside a non-editable host no longer forces a synchronous style recalculation in Chrome.

'lexical': patch
---

[perf] Reconciling a decorator host with named slots no longer rewrites an unchanged `contentEditable` on its slot containers, which in Chrome forced a synchronous style recalculation on every such reconcile

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: the skip lives in the exported $markSlotEditable, so it also covers slot containers in a contentEditable=false element shell and apps that call it from updateDOM (as the named-slots docs recommend). Naming the function would reach those callers, e.g. [perf] $markSlotEditable no longer rewrites an unchanged contentEditable, so reconciling a slot host inside a non-editable host no longer forces a synchronous style recalculation in Chrome.

@emilyhorsman
emilyhorsman added this pull request to the merge queue Nov 3, 2026
Merged via the queue into main with commit b7aab81 Nov 3, 2026
57 checks passed
@emilyhorsman
emilyhorsman deleted the emilyhorsman/skip-unchanged-contenteditable branch November 3, 2026 06:54
@potatowagon potatowagon mentioned this pull request Dec 2, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 79ab8849 Deployed Nov 3, 2026 by vercel[bot]
Preview – lexical-playground — 79ab8849 Deployed Nov 3, 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.

2 participants