Legend-State is designed to be the fastest React state library, beating other libraries on nearly every benchmark. This guide explains how Legend-State achieves its performance and how you can optimize your application.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/LegendApp/legend-state/llms.txt
Use this file to discover all available pages before exploring further.
Why Legend-State is Fast
Legend-State achieves its exceptional performance through several key design decisions:Fine-Grained Reactivity
Legend-State lets you make your renders super fine-grained, so your apps are faster because React has to do less work. The best way to be fast is to render less, less often.Optimized Architecture
Legend-State uses several architectural optimizations:- Proxy-based observables: Efficient tracking with minimal overhead
- Lazy activation: Observable nodes activate only when accessed
- Batched updates: Multiple changes are batched into a single notification
- Smart array handling: Optimized for large lists with minimal re-renders
Minimal Bundle Size
At only 4kb, Legend-State has a tiny footprint that won’t bloat your bundle. Combined with minimal boilerplate, you’ll see significant file size savings.Benchmark Results
Legend-State beats every other state library on just about every metric and is so optimized for arrays that it even beats vanilla JS on the “swap” and “replace all rows” benchmarks.See the official benchmarks for detailed performance comparisons.
Performance Optimization Techniques
1. Use Fine-Grained Reactivity
Prefer using reactive components over wrapping entire components withobserver:
2. Leverage Lazy Activation
Observables only activate when observed. This means creating or setting large objects is very fast:3. Optimize Array Rendering
Legend-State has special optimizations for arrays that can even beat vanilla JavaScript:How array optimization works
How array optimization works
Legend-State tracks array changes at a granular level:
- Adds/removes: Only new/removed items re-render
- Reorders: Items maintain their identity and position
- Updates: Only changed items re-render
For component uses shallow tracking by default and can detect moves, swaps, and replacements efficiently.4. Use peek() for One-Time Reads
When you need to read a value without tracking it, usepeek():
5. Batch Updates
Multiple changes are automatically batched, but you can explicitly batch for better control:6. Use Shallow Tracking
For arrays and objects where you only care about shallow changes:7. Computed Values
Use computed observables for derived state that only re-computes when dependencies change:In v3, computeds only re-compute when observed. They won’t run unless something is actively listening to them.
Performance Anti-Patterns
❌ Overusing observer
❌ Reading entire objects
❌ Creating observables in render
❌ Not using keys in lists
Benchmark Categories
Legend-State optimizations fall into three categories:Architecture Optimizations
Related to the core design, including how
Proxy is used:- Lazy node activation
- Efficient tracking system
- Minimal overhead proxies
Micro-Optimizations
Related to JavaScript primitives:
- Efficient iteration
- Optimized object types
- Minimal function calls
Real-World Performance Tips
Large Lists
For lists with thousands of items:Complex Forms
For forms with many fields:Conditional Rendering
Use theShow component for efficient conditional rendering:
Measuring Performance
Use React DevTools Profiler to measure the impact of your optimizations:- Enable “Highlight updates when components render”
- Look for unnecessary re-renders
- Measure render duration
- Compare before/after optimization
Summary
Key Takeaways:
- Use fine-grained reactivity with
Reactivecomponents - Leverage lazy activation for large objects
- Use
Forcomponent for optimized lists - Use
peek()for untracked reads - Batch updates when making multiple changes
- Avoid reading entire objects when you only need specific properties
Next Steps
- Learn about TypeScript integration
- Explore React Native setup
- Read the Migration Guide