Repository navigation
[lexical] Feature: build the node with $create when importing JSON - #9090
Merged
etrepum merged 1 commit intoAug 23, 2026
Merged
Conversation
## Description The synthesized `importJSON` builds its node with `$applyNodeReplacement(new klass())`, which constructs the replaced node, discovers a replacement, constructs the replacement too, and drops the first — one wasted construction per node, and constructing a node is most of what importing one costs. The `TODO` left in that spot by meta#9061 asks for the switch to `$create`, which resolves `withKlass` first and constructs once, which is what it was written for. What kept the TODO there is the one replacement `$create` could not express: a `replace` given without a `withKlass` cannot be resolved ahead of construction, because only its `with` function knows what to build, and `$create` documented itself as ignoring those. So `$create` no longer ignores them. The resolve already stops at a registered node whose `replace` is still set — that is exactly the deprecated case, with no `withKlass` left to follow — so honoring it is one comparison on a value already in hand, and needs nothing recorded on the editor. With that, `importJSON` can call `$create` and the TODO goes away. Which also makes `$create` agree with the `$create*` factories for a configuration where it previously did not: with a deprecated `replace` registered, `$create(ParagraphNode)` and `$createParagraphNode()` both give you the replacement now. ## Test plan ### Before The new `$create` case fails, since a `with` without a `withKlass` was ignored: ``` $ npx vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx -t 'create resolves withKlass' FAIL |unit| packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx > LexicalEditor tests > node replacement > $create resolves withKlass directly, and still honors a `with` without one AssertionError: expected ParagraphNode{ …(17), …(1) } to be an instance of LegacyParagraph ❯ legacy.update.discrete packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx:4045:42 4043| legacy.update( 4044| () => { 4045| expect($create(ParagraphNode)).toBeInstanceOf(LegacyParagrap… | ^ Test Files 1 failed (1) Tests 1 failed | 92 skipped (93) ``` And switching `importJSON` to `$create` on its own — without teaching `$create` about the deprecated form — breaks the case meta#9061 added: ``` $ npx vitest run --project unit packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx -t 'applies the replacement on import' - Expected + Received - true + false ❯ packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx:4088:58 4086| newEditor.read(() => { 4087| const paragraph = $getRoot().getFirstChild(); 4088| expect(paragraph instanceof CustomParagraphNode).toBe(true); | ^ Test Files 1 failed (1) Tests 1 failed | 92 skipped (93) ``` Parsing a 12,001 node document whose text nodes carry the base `text` type against a registered `TextNode` replacement, production build, min of 25 after 5 warmup iterations, three runs: ``` no replacement 72.11 / 71.56 / 70.60 ms replaced, withKlass 93.72 / 93.31 / 95.06 ms replaced, legacy (no withKlass) 91.88 / 95.21 / 96.00 ms ``` ### After Both cases pass, along with the rest of the suite: ``` $ npx vitest run --project unit Test Files 256 passed (256) Tests 4241 passed | 1 skipped (4242) $ pnpm run test-browser Test Files 18 passed (18) Tests 131 passed | 1 skipped (132) $ pnpm run ci-check No errors! $ echo $? 0 ``` Same benchmark, same machine, three runs: ``` no replacement 73.84 / 71.92 / 72.38 ms replaced, withKlass 75.57 / 73.41 / 72.73 ms replaced, legacy (no withKlass) 96.21 / 94.81 / 95.90 ms ``` About 22% off a parse when a `withKlass` replacement is registered, and the no-replacement and deprecated-replacement cases are unchanged within run-to-run noise — the deprecated one still pays for the discarded construction, because it is the case that cannot avoid it. One test is new, covering `$create` in both configurations. The E2E matrix (Chromium/Firefox/WebKit) was not exercised, since it needs the playground dev server. Co-Authored-By: Claude Opus 5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
marked this pull request as ready for review
August 23, 2026 00:47
etrepum
requested review from
acywatson,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 23, 2026 00:47
zurfyx
approved these changes
Aug 23, 2026
unix-max
pushed a commit
to unix-max/lexical
that referenced
this pull request
Aug 28, 2026
…eta#9090) Co-authored-by: Claude <[email protected]>
Merged
This was referenced Sep 9, 2026
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
Follow-up to #9061 to replace
$applyNodeReplacement(new klass())with$create(klass)in importJSON by making$createcompatible with node replacements that are missingwithKlass.In situations where node replacement with
withKlassthis halves the cost of importing a node. The majority of the cost is in the constructor, and$applyNodeReplacementconstructs two nodes when any replacement is configured.$createonly constructs a single node whenwithKlassis specified for the entire chain.Test plan
New unit tests to confirm that
$createdirectly handles replacement for configurations usingwithwithoutwithKlass.