Patch Changes
-
Significantly improve localStorage collection performance during rapid mutations (#760)
Optimizes localStorage collections to eliminate redundant storage reads, providing dramatic performance improvements for use cases with rapid mutations (e.g., text input with live query rendering).
Performance Improvements:
- 67% reduction in localStorage I/O operations - from 3 reads + 1 write per mutation down to just 1 write
- Eliminated 2 JSON parse operations per mutation
- Eliminated 1 full collection diff operation per mutation
- Leverages in-memory cache (
lastKnownData) instead of reading from storage on every mutation
What Changed:
- Mutation handlers now use in-memory cache instead of loading from storage before mutations
- Post-mutation sync eliminated - no longer triggers redundant storage reads after local mutations
- Manual transactions (
acceptMutations) optimized to use in-memory cache
Before: Each mutation performed 3 I/O operations:
loadFromStorage()- read + JSON parse- Modify data
saveToStorage()- JSON stringify + writeprocessStorageChanges()- another read + parse + diff
After: Each mutation performs 1 I/O operation:
- Modify in-memory data ✨ No I/O!
saveToStorage()- JSON stringify + write
Safety:
- Cross-tab synchronization still works correctly via storage event listeners
- All 50 tests pass including 8 new tests specifically for rapid mutations and edge cases
- 92.3% code coverage on local-storage.ts
lastKnownDatacache kept in sync with storage through initial load, mutations, and cross-tab events
This optimization is particularly impactful for applications with:
- Real-time text input with live query rendering
- Frequent mutations to localStorage-backed collections
- Multiple rapid sequential mutations