A guide to optimizing your Reflex application for maximum performance.
Only update values when necessary.
✅ Good:
// Updates only when value significantly changes
const position = reflex({
initialValue: { x: 0, y: 0 },
equals: (prev, next) =>
Math.abs(prev.x - next.x) < 0.1 &&
Math.abs(prev.y - next.y) < 0.1
});
❌ Bad:
// Updates on every tiny change
position.setValue({ x: mouse.x, y: mouse.y });
Group related updates to reduce the number of notifications.
✅ Good:
Reflex.batch(() => {
user.name.setValue('John');
user.email.setValue('[email protected]');
user.preferences.setValue({ theme: 'dark' });
});
❌ Bad:
// Three separate update cycles
user.name.setValue('John');
user.email.setValue('[email protected]');
user.preferences.setValue({ theme: 'dark' });
Keep computed values efficient and minimal.
✅ Good:
// Efficient computation
const visibleItems = computed(
[items, filter],
([items, filter]) => items.filter(item => item.type === filter)
);
❌ Bad:
// Expensive computation on every update
const visibleItems = computed(
[items, filter],
([items, filter]) => {
return items
.map(expensiveTransform)
.filter(item => item.type === filter)
.sort(expensiveSort);
}
);
Use memoization for expensive operations.
✅ Good:
const memoizedTransform = memoize(expensiveTransform);
const processedItems = computed(
[items],
([items]) => items.map(memoizedTransform)
);
Remove subscriptions that are no longer needed.
✅ Good:
class Component {
private cleanup: Array<() => void> = [];
init() {
this.cleanup.push(
value1.subscribe(this.handler1),
value2.subscribe(this.handler2)
);
}
destroy() {
this.cleanup.forEach(fn => fn());
this.cleanup = [];
}
}
Be careful with closures and references.
✅ Good:
class Component {
private handler = (value: number) => {
// Handler bound to instance
this.update(value);
};
init() {
return value.subscribe(this.handler);
}
}
❌ Bad:
class Component {
init() {
// Creates new closure on every init
return value.subscribe(value => {
this.update(value);
});
}
}
Control high-frequency updates with appropriate operators.
✅ Good:
// Throttle rapid updates
const throttledMouse = mousePosition.throttle(16); // ~60fps
// Buffer and batch process updates
const bufferedUpdates = updates.buffer({
size: 10,
time: 1000
});
// Sample at regular intervals
const sampledValue = value.sample(1000);
Delay processing of rapid user input.
✅ Good:
const searchTerm = reflex({ initialValue: '' });
const debouncedSearch = searchTerm.pipe(
debounce(300),
filter(term => term.length >= 3)
);
Be selective with deep reactivity.
✅ Good:
// Only track necessary paths
const user = deepReflex({
initialValue: {
profile: { name: '', settings: {} },
// Non-reactive data
staticData: { /* ... */ }
},
paths: ['profile.name', 'profile.settings']
});
Don't use deep comparison when unnecessary.
✅ Good:
const list = reflex({
initialValue: [],
equals: (prev, next) => prev.length === next.length
});
Create efficient framework integrations.
✅ Good:
// React optimization
function useReflex<T>(value: Reflex<T>): T {
const [state, setState] = useState(value.getValue());
useEffect(() => {
// Only subscribe once
return value.subscribe(setState);
}, [value]); // Proper dependency
return state;
}
Prevent unnecessary UI updates.
✅ Good:
// React component with optimization
const TodoItem = memo(function TodoItem({ todo }: Props) {
const item = useReflex(todo);
return <li>{item.text}</li>;
});
Add performance monitoring in development.
✅ Good:
const monitoredValue = reflex({
initialValue: 0,
middleware: [
value => {
performance.mark('value-update-start');
const result = expensiveOperation(value);
performance.mark('value-update-end');
performance.measure(
'value-update',
'value-update-start',
'value-update-end'
);
return result;
}
]
});
Write performance tests.
✅ Good:
describe('Performance', () => {
it('should handle rapid updates efficiently', () => {
const value = reflex({ initialValue: 0 });
const start = performance.now();
for (let i = 0; i < 1000; i++) {
value.setValue(i);
}
const end = performance.now();
expect(end - start).toBeLessThan(100);
});
});
- Review the Best Practices guide for more optimization tips
- See the Advanced Usage Guide for complex patterns
- Check the API Reference for detailed method documentation