Document Structure
Organizing your Y.Doc for maintainable and efficient collaborative applications.
Overview
A Y.Doc is the root container for all collaborative data. Structure affects performance, maintainability, and how concurrent edits merge.
Structure by Purpose
Organize data into distinct categories:
Example: Spreadsheet
Example: Canvas App
Example: Presentation
Design Guidelines
Separate Content from Order
- Store content in Y.Map by ID
- Store ordering in Y.Array (IDs only)
- Never put complex objects in reorderable arrays
Flatten When Possible
- Deep nesting has overhead
- Prefer 2-3 levels max
- Use flat maps with composite keys when appropriate
Use TypeScript Interfaces
- Define types for your document structure
- Create helper functions for typed access
Initialize Required Structure
- Access top-level types on document creation
- Set defaults in a transaction
Performance Notes
- Many top-level types is fine—enables granular sync
- Deeply nested types (4+ levels) add overhead
- Large arrays (10,000+ items): consider pagination or chunking
See Also
- Separate Content Maps - Splitting data for performance
- ID-Based Storage - Content/order separation pattern
- Shared Types - Understanding each type’s behavior