diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..e48acac --- /dev/null +++ b/.jules/bolt.md @@ -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. diff --git a/script.js b/script.js index bdc06f3..c79d55d 100644 --- a/script.js +++ b/script.js @@ -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) { @@ -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(); }