Overview
TanStack Store is built on alien-signals, a high-performance reactive system that provides automatic optimization for most use cases. This guide covers advanced performance patterns for building highly efficient applications.The Reactive Core
TanStack Store uses a fine-grained reactive system that tracks dependencies automatically:- Automatic dependency tracking: Only recomputes when dependencies change
- Diamond dependency resolution: Efficiently handles complex dependency graphs
- Lazy evaluation: Derived stores only compute when accessed
- Minimal re-renders: Subscribers only fire when values actually change
Batching Updates
Why Batch?
Batching multiple state updates prevents unnecessary intermediate computations and notifications:Batch API
Thebatch function defers notifications until all updates complete:
Batching Best Practices
Batch Related Updates
Group state updates that logically belong together:
Batch in Event Handlers
Use batching in user interactions:
Fine-Grained Subscriptions
Selective Updates
Subscribe only to the data you need to avoid unnecessary re-renders:Derived Store Optimization
Derived stores automatically optimize by only recomputing when their dependencies change:Selector Patterns
Creating Efficient Selectors
Create focused derived stores that select specific slices of state:Memoization with Selectors
Selectors act as automatic memoization:Diamond Dependency Optimization
TanStack Store efficiently handles diamond dependencies (where multiple derived stores depend on the same source):Complex Diamond Dependencies
The reactive system handles even complex dependency graphs efficiently:Comparison Functions
Custom Equality Checks
Provide custom comparison functions to control when updates trigger:Shallow Comparison
For array and object stores, use shallow comparison when appropriate:Performance Measurement
Tracking Computations
Monitor how often derived stores recompute:Benchmarking with Vitest
TanStack Store includes benchmarks for performance testing:Best Practices
1
Use Derived Stores for Computed Values
Don’t store computed values directly. Let derived stores handle them:
2
Batch Related Updates
Always batch updates that happen together:
3
Create Focused Selectors
Build small, focused derived stores instead of one large one:
4
Use Custom Comparisons Sparingly
Only use custom comparison functions when necessary. The default
Object.is comparison works well for most cases.Common Performance Pitfalls
Profiling Tools
Browser DevTools
Use React DevTools Profiler to identify unnecessary re-renders:- Open React DevTools
- Go to Profiler tab
- Record interactions
- Look for components that re-render when they shouldn’t
Performance Testing
Write performance tests to catch regressions:Next Steps
- Learn about Migration from other state libraries
- Explore Type Safety for better DX
- Check out batch API for batching details