Feature: Scaling Lexical for large documents using chunked multi-editor architecture
Description
I’m building a large document editor (Word/Google Docs-like) using Lexical.
Problem
I need to support:
~300–1000 page documents lossless import/export (HTML/structured docs) smooth editing on low-end machines
A single Lexical editor becomes heavy at this scale due to large EditorState and DOM size.
Proposed architecture (summary)
I’m considering:
A canonical document model as source of truth Chunk engine to split document into block-based units Viewport-based virtualization Multiple LexicalComposer instances mounted only for visible chunks Separate layout engine for page view rendering
Full design reference:
Word-Class Document Editor Architecture
This document describes the architecture of a scalable, Word-class document editor designed for large documents, lossless imports, and high-performance editing on low-end machines.
1. Core Principle
The Document Model is the only source of truth.
Everything else in the system is a projection:
- Lexical ? editing layer
- Chunk Engine ? performance partitioning
- Layout Engine ? page/web rendering
- UI ? visual representation
No other layer owns canonical data.
2. High-Level Architecture
Document Model (Source of Truth) ? Chunk Engine (Storage Partitioning) ? Viewport Virtualization (Performance Control) ? Mounted Lexical Editors (Editing Runtime) ? Layout Engine (Page/Web Rendering) ? UI Renderer
- Document Model (Core Layer)
Purpose
The Document Model is the canonical representation of all content.
Structure
Syntax:
Document { blocks: Block[] }
Block { id: string type: "paragraph" | "heading" | "table" text: string styles: StyleSpan[] }
Responsibilities
- Store all document content
- Maintain stable IDs
- Track structure and formatting
- Remain independent of UI and rendering
Rules
- No page information
- No chunk information
- No DOM / Lexical dependency
4. Chunk Engine (Storage Layer)
Purpose
The Chunk Engine divides large documents into manageable units for performance optimization.
Structure
syntax Chunk { id: string blockIds: string[] dirty: boolean version: number }
Responsibilities
- Partition document into chunks (e.g., 50–200 blocks per chunk)
- Track dirty state for updates
- Enable lazy loading and unloading
- Improve performance for large documents
Rules
- NOT related to pages
- NOT related to layout
- NOT tied to Lexical or UI
5. Viewport Virtualization Layer
Purpose
Controls which chunks are actively mounted in the editor.
Behavior
Only chunks in or near the viewport are active:
Visible: Chunk 48 Chunk 49 Chunk 50
Mounted: 3–5 Lexical editors
Unloaded: Remaining chunks
Responsibilities
- Mount/unmount Lexical editors dynamically
- Manage scroll-based loading
- Maintain constant memory usage
6. Lexical Layer (Editing Runtime)
Purpose
Lexical is the interactive editing surface.
Key Concept
Lexical is NOT the document. It is a projection.
Structure
Each mounted chunk becomes a Lexical instance:
Responsibilities
- Handle typing and input
- Manage cursor and selection
- Process IME input
- Maintain local editing state
Output
Lexical emits commands:
UpdateBlockText(blockId, newText) InsertBlock(...) DeleteBlock(...)
7. Layout Engine (Page/Web Rendering)
Purpose
Computes visual representation of the document.
Key Concept
Pages are computed, not stored.
Structure
PageMap { blockId ? pageIndexes[] }
Responsibilities
- Line breaking
- Page breaking
- Pagination rules
- Reflow calculations
- Rendering layout updates
Example
Before edit:
Page 5: Paragraph B
Page 6: Paragraph C
After edit:
Page 5: Paragraph B (partial)
Page 6: remainder of B Paragraph C
8. System Flow (Write Operation)
When a user types:
Step 1: Input in Lexical
User modifies text inside active chunk.
Step 2: Lexical updates local state
Chunk-level editor updates.
Step 3: Command emitted
UpdateBlockText(blockId, newText)
Step 4: Document Model update
Canonical state is updated.
Step 5: Chunk marked dirty
Affected chunk flagged for recomputation.
Step 6: Layout recalculation
Only affected region is recomputed.
Step 7: UI updates
Pages and visible content are re-rendered.
9. Page View System
Key Principle
Pages are a visual projection, not stored data.
Flow
Document Model ? Layout Engine ? Page Mapping ? UI Renderer
Behavior
Typing can move content across pages without modifying:
- chunk structure
- document model
- Lexical structure
Only layout mapping changes.
10. Design Rules
Rule 1
Chunks ? Pages
Rule 2
Lexical ? Source of Truth
Rule 3
Pages are computed, not stored
Rule 4
Only Document Model is permanent state
Rule 5
Chunking is a performance optimization, not a structural model
11. Architecture Diagram
+--------------------+
¦ Document Model ¦
¦ (Source of Truth) ¦
+--------------------+
¦
+-------------------------------+
¦ ¦
+------?------+ +-------?-------+
¦ Chunk Engine ¦ ¦ Layout Engine ¦
¦ (Storage) ¦ ¦ (Pagination) ¦
+--------------+ +---------------+
¦ ¦
? ?
+--------------+ +----------------+
¦ Lexical Pool ¦ ¦ Page Renderer ¦
¦ (Mounted) ¦ ¦ (Virtual UI) ¦
+--------------+ +----------------+
12. System Goals
This architecture enables:
- High-performance editing of large documents
- Constant memory usage under load
- Lossless import/export pipelines
- Page + web view rendering
- Future support for collaboration and track changes
13. Summary
This system separates concerns into four independent layers:
- Document Model ? truth
- Chunk Engine ? scalability
- Lexical Layer ? editing
- Layout Engine ? rendering
This separation ensures long-term scalability and maintainability for enterprise-grade document editing systems.
Additional question
If anyone has tried similar approaches, what were the biggest issues (selection sync, undo consistency, performance, etc.)?
I don’t think something like this makes sense in lexical, and it looks like AI hand-waving and not a realistic plan based on how things actually work.
What would make sense is to improve the reconciler to be able to virtualize large documents. A useful contribution would be examples with large realistic documents that don’t work well with the current architecture.
Thanks for the feedback. That's fair — I may have focused too much on a specific implementation idea rather than the underlying problem.
The issue I'm running into is with very large real-world documents. For example, importing a ~30 MB finance document (100+ pages) containing large tables, custom paragraph metadata, images, Yjs collaboration data, and other complex nodes can take ~28 seconds. After import completes, the page remains unresponsive for another 30–40 seconds before it becomes usable, and editing performance is still noticeably laggy.
My main goal isn't necessarily a specific virtualization approach, but finding ways for Lexical to handle documents of this size more gracefully. If improving the reconciler isn't the right direction, it would be useful to understand which parts of the architecture become the bottleneck in these scenarios and what patterns are recommended for large-document workloads.
I can also try to put together a reproducible example or benchmark document if that would be helpful.
The only correct way to make things better is to start with an actual use case, profile it to see what the bottlenecks are, and then make improvements. Otherwise you’re just doing random stuff that could even be making things worse.
The default importDOM code is not performant, which is one reason why we have a new DOMImportExtension that takes a much more efficient approach. For large regular documents you might be better off rolling your own serialization specialized to the use case, all of the flexibility is not great for performance (in time or size).
yjs is not really helping either, CRDTs are good for the offline distributed use case but have a lot of overhead if you don’t have a centralized way to do snapshots and basically replace the document with a minimal version.
I ran some benchmarks in real Chromium to get a baseline for Lexical core at this scale — complex documents with paragraphs, table-like shadow root structures, and decorator nodes:
107k nodes (1000 pages):
| Phase | Time |
|---|---|
| EditorState construction | 146ms |
| + Reconciler (DOM creation) | 211ms |
| + Node transforms + history | 225ms |
| Browser reflow | 269ms |
$generateNodesFromDOM (3.8MB HTML) |
300ms |
Lexical core with transforms tops out around 700ms for 107k nodes, which suggests ~97% of the 28s is outside the core — consistent with what etrepum said about importDOM overhead and Yjs.
One caveat: the transforms in this benchmark are lightweight (property checks, sibling reads). Real-world transforms that do DOM queries, regex matching, or external state sync per node would multiply across 107k dirty nodes and could add significantly more. A Chrome DevTools Performance trace on the actual import should make the real bottleneck obvious.
By the way, I was going through some old issues https://github.com/facebook/lexical/issues/5139 and noticed something interesting. Pasting 20,000 identical paragraphs via the clipboard takes about 4 seconds. Tested on Chrome, MacBook M4
- Content Playground 2026-06-25T00_07_39.531Z.lexical.zip
- Chrome perfomance traces Trace-20260625T025922.json.gz
The copy and cut operation also takes a long time. But deletion works quickly
The playground isn't really a good testbed for large document performance, it is the kitchen sink. Has all sorts of stuff turned on that would slow things down (the tree view for example), lots of code that is just a proof of concept and not optimized, it's deployed with dev rather than prod, etc.
Would be less noisy to use a more minimal harness that is representative of the actual intended use case.
Thanks for the guidance. We did additional testing based on your recommendation and tried to isolate the bottlenecks.
For testing purposes, we switched to the new DOMImportExtension approach and completely disabled Yjs/realtime synchronization. Even with Yjs removed from the equation, importing a large document still takes around 30 seconds. After import completes, the editor remains largely unresponsive for some time, and once editing becomes possible, there is noticeable lag during normal operations.
Document metrics:
- Total nodes/elements: 519,352
- Paragraph nodes: 484,485
- Tables: 1,570
- Table rows: 18,523
Given these numbers, we're trying to understand where the practical limits are and what optimization strategies are recommended for documents of this scale.
A few questions:
- Are there known performance constraints in Lexical when working with documents containing ~500k nodes?
- Does Lexical support any form of viewport-based virtualization for large documents, or is rendering the entire editor state currently expected?
- Are there recommended approaches for chunking/paginating content while preserving editing functionality?
- Beyond import performance, are there specific profiling areas (reconciliation, DOM creation, selection handling, table rendering, etc.) that tend to become dominant bottlenecks at this document size?
- Is the general recommendation to move toward a custom serialization/import pipeline and a document model that avoids loading the entire document into a single Lexical editor instance?
We're happy to provide profiling traces if there are specific metrics that would be most useful.
Lexical currently has no built-in virtualization. If it’s in an editor attached to the DOM as a node then the node has DOM associated with it. It could be done, but nobody has needed it badly enough, or they have managed by using multiple editors and simply haven’t talked about how they did it.
Most people use lexical with much smaller documents so none of your questions have real answers or recommendations. If you want to make it work well then you’d have to contribute significant engineering effort either directly or indirectly to measure and iterate on changes until it does.
Traces are not particularly helpful, you can’t measure changes against traces. Concrete examples would be more useful, but know that your problems are not the same ones everyone else has so it’s not necessarily anyone else’s priority to do this optimization work.
Thanks for the clarification.
My main concern is improving performance for very large documents. I understand Lexical doesn't currently have built-in virtualization and that most users don't hit this limitation.
My question is specifically whether it's technically feasible to implement virtualization while keeping a single Lexical editor instance, or whether the recommended direction for very large documents is to split the content across multiple editor instances. I'm trying to understand which architecture would be more practical before investing engineering effort.
Either would be a challenge, just different challenges. Both are technically feasible and would require significant expertise to do well. If you make a requirement that you can't change lexical's internals then your only option is to do some sort of partitioning with multiple editors. There is no guidebook for how to do this because nobody has done it and written publicly about it.
However, partitioning into multiple editors is going to have more edge cases and likely end up being more code than improving lexical's core to include virtualization and eliminating other bottlenecks that occur with large documents. That said, making either of these easier is not a primary focus for any contributor today, so there is no timeline or concrete answers. Since it's not a currently focus, if you want assistance in the near term then you'd have to drive the effort by providing very concrete examples and likely doing some of the work yourself.
Thanks for the detailed explanation. That helps clarify the trade-offs between partitioning and modifying Lexical itself.
I'm leaning toward exploring a partitioned architecture first, since I would prefer not to fork or modify Lexical's internals initially.
One question I have is about state management. If each partition is backed by a shared external store (for example, Zustand) and only the visible partitions are mounted as Lexical editors, would that meaningfully reduce browser layout/reconciliation overhead? Or is the primary bottleneck still the DOM and Lexical's internal processing, making external state management largely orthogonal to the performance problem?
I'm happy to build a proof of concept and share concrete findings if that would be helpful.
Nobody knows what the overhead is because nobody has done what you are trying to do and written about it. Lexical has also changed a lot recently so even if they did it a month or two ago those results may not reflect the status quo (due to optimizations and new features). If you want to know you’ll have to build and profile it.
State management can still be done with lexical, you’d use a shallow tree of editors (probably just two levels) rather than entirely distinct editors. You don’t need anything else. Just a lot of code to get the UX and serialization/interchange correct.
Thanks for the clarification. That makes sense—without a benchmark, any discussion about overhead would mostly be speculative, especially given the recent changes in Lexical.
My main concern isn't trying to avoid profiling, but understanding whether this architecture is fundamentally reasonable before investing time in building a prototype.
Regarding the shallow tree of editors, could you elaborate on what you mean by "two levels"? For example, are you suggesting a root editor that owns a small number of section/sub-document editors, rather than one independent Lexical instance per block or document?
I'll prototype and profile both approaches, but I'd appreciate any guidance on the architecture that aligns best with how Lexical is intended to scale.
Lexical is “intended” to be used by one editor to own the document. Nested editors is a mostly a workaround to allow DecoratorNode to have “children” or temporary editing surfaces. This workaround is no longer necessary for most use cases since the introduction of Named Slots.
Yes by two levels I mean one editor that owns the entire editing surface. It contains custom DecoratorNodes to manage and render nested editors (possibly 1:1 but maybe 1:N).
But like I said before, it’s probably less code to improve lexical’s performance by modifying the core (to implement virtualization and optimizations) than make partitioned multi-editor documents work seamlessly. Either would be a considerable effort, without much overlap.
Thanks, that's very helpful. It sounds like you're recommending optimizing a single editor rather than partitioning the document into many editors.
When you mention implementing virtualization and other optimizations in Lexical's core, could you elaborate on what that might look like?
For example, would you envision:
- virtualizing the rendered DOM while keeping the full editor state in memory,
- avoiding reconciliation for off-screen nodes,
- lazy mounting/unmounting portions of the document,
- or something else entirely?
I'm trying to understand what architectural direction you'd recommend for supporting very large documents while still keeping a single editor instance.
I wouldn’t pick any strategy until there are measurements to direct the efforts. You can’t make something fast if you don’t know why it’s slow.
Thanks, I did some profiling to better understand where the performance bottleneck is before experimenting with different architectures. I'm testing Lexical editor with a large imported document (~35 MB, and we also have larger documents around 60 MB). Using Chrome Performance traces while typing a single character, I consistently see: " Browser Layout is the dominant cost (~2.3 s in one representative trace). " Lexical reconciliation ($reconcileRoot) is around 75 ms. " My custom selection extension adds roughly 80 ms. " The trace shows most of the time is spent in browser layout before Lexical's update work completes. I also inspected the rendered DOM: " ~1.4 million DOM elements inside the editor " ~460k
elements " ~352k elements From the traces, it looks like the browser's layout engine is the primary bottleneck rather than Lexical reconciliation itself. Earlier you mentioned that implementing virtualization in Lexical's core would likely be a better direction than partitioning the document into multiple editors. Given these measurements, would you still recommend pursuing core-level virtualization? More specifically: " Is the expectation that Lexical would eventually support keeping the full EditorState while mounting only the visible DOM? " Or are there other architectural optimizations you'd investigate first for documents of this size? I'm not looking for implementation details yet-I'm mainly trying to understand what direction you'd recommend before investing significant engineering effort.
Yes I would first look to see what can be optimized with browser layout, could be as simple as changing the CSS to use more efficient rules. Next I would try virtualizing the DOM that the browser has to layout, it’s likely that if most of the off-screen blocks are display: none then there would be much less to do.
Virtualizing what is reconciled by lexical would be another step after that only if it’s useful, and might include making the reconciler a property of the editor that can be replaced in order to not pay for the virtualization complexity when not using it.
I haven’t looked at the implementation at all but there is a new attempt at virtualization for paginated documents here https://discord.com/channels/953974421008293909/1522228911193460827 https://github.com/rt83/paginary