JSON vs Y.Map Tradeoffs
Choosing between plain JSON objects and nested Y.Maps is a critical design decision.
The Core Tradeoff
| Aspect | JSON Objects | Nested Y.Maps |
|---|---|---|
| Update granularity | Whole object replaced | Per-field |
| Concurrent field edits | Last-write-wins (data loss!) | Merge correctly |
| Memory overhead | Lower | Higher (CRDT metadata) |
| Code complexity | Simpler | More complex |
The Concurrent Edit Problem
This is the critical consideration. With JSON objects, concurrent field edits cause data loss.
With Y.Map, both changes merge correctly → { name: 'Bob', score: 200 }
Decision Framework
Use JSON when:
- Thousands of small entities (memory matters)
- Entities rarely updated after creation
- Users won’t edit same entity simultaneously
- Values are primitives
Use Y.Map when:
- Concurrent editing of same entity is likely
- Entities have many fields
- Frequent partial updates
- Field-level merging matters
Hybrid: Separate by Volatility
Different parts of entities may have different update patterns:
Memory Impact
| Structure | ~Overhead per item |
|---|---|
| JSON in Y.Map | ~50 bytes |
| Nested Y.Map (5 fields) | ~300 bytes |
For 10,000 items: ~500KB (JSON) vs ~3MB (Y.Map)
Guidelines Summary
| Scenario | Recommendation |
|---|---|
| Large collection of small values | JSON |
| Complex objects, unlikely concurrent edits | JSON |
| Complex objects, likely concurrent edits | Y.Map required |
| Mixed update patterns | Separate maps by volatility |
See Also
- Separate Content Maps - Organizing by volatility