Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

## 2026-03-04 - [Caching localStorage for High-Frequency Loops]
**Learning:** Found a performance bottleneck where `localStorage.getItem` and `JSON.parse()` were being called synchronously inside `getFavorites()`, which was executed on every keystroke during `renderPDFs` search filtering.
**Action:** Always cache expensive synchronous operations (like `localStorage` parsing) in a variable when they are accessed in high-frequency rendering or filtering loops. Ensure state updating functions like `toggleFavorite` update both the cache and persistent storage.
10 changes: 9 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1315,9 +1315,14 @@ async function handleCommentSubmit(e) {
/* =========================================
10. EXTRAS (THEME, FAVORITES, EASTER EGGS)
========================================= */
// ⚑ Bolt Optimization: Cache favorites to avoid synchronous localStorage reads and JSON.parse on every render
let favoritesCache = null;

function getFavorites() {
if (favoritesCache !== null) return favoritesCache;
const stored = localStorage.getItem('classNotesFavorites');
return stored ? JSON.parse(stored) : [];
favoritesCache = stored ? JSON.parse(stored) : [];
return favoritesCache;
}

function toggleFavorite(event, pdfId) {
Expand All @@ -1336,6 +1341,9 @@ function toggleFavorite(event, pdfId) {
favorites.push(pdfId);
showToast('Added to saved notes');
}

// Update cache before saving
favoritesCache = favorites;
localStorage.setItem('classNotesFavorites', JSON.stringify(favorites));
renderPDFs();
}
Expand Down