Overview
Computed observables are reactive values derived from other observables. They automatically recalculate when their dependencies change and only re-run when accessed. This makes them highly efficient for deriving data.Creating Computed Observables
Using observable() with Functions
The simplest way to create a computed observable is to pass a function toobservable():
Using the computed() Function
Thecomputed() function provides a more explicit syntax:
Type Signatures
Lazy Evaluation
Computed observables are lazy - they don’t run until accessed:Two-Way Computed
Create computed observables that can be both read and written:Computed with Multiple Sources
Computed Objects
Computed observables can return objects, making nested properties observable:Advanced Patterns
Computed from Arrays
Create computed values from array operations:Filtered Lists
Chained Computed
Computed observables can depend on other computed observables:Computed with peek()
Usepeek() to read values without creating dependencies:
Self-Referencing Computed
Access the previous computed value:Memoized Selectors
Performance Optimization
Avoiding Unnecessary Recomputation
Computed observables only recompute when:- They are accessed (lazy evaluation)
- At least one dependency has changed
- Someone is observing them
Breaking Down Complex Computations
Split complex computations into multiple computed observables:Inline Computed Properties
Define computed properties inline with your state:Computed with Promises
Computed observables can handle async operations:Best Practices
Keep Computations Pure
Computed functions should not have side effects - only transform data.
Use peek() for Config
Use
peek() for configuration values that shouldn’t trigger recomputation.Break Down Complex Logic
Split complex computations into multiple computed observables for better caching.
Avoid Heavy Computations
For expensive operations, consider debouncing or using async computed.
Common Patterns
Sorting and Filtering
Aggregations
Validation
Next Steps
Batching
Learn how to optimize multiple updates
React Integration
Use computed observables in React components