From be46fa6794dc38f967118fc4e7bb9e025db28bf6 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 4 Mar 2026 13:47:42 +0000 Subject: [PATCH] perf: cache favorites to prevent synchronous localStorage reads Implements an in-memory cache for the user's favorites list to avoid executing `localStorage.getItem` and `JSON.parse` synchronously during the high-frequency `renderPDFs` search filtering loop. Updates `toggleFavorite` to update both the cache and persistent storage. Adds a learning entry to the bolt journal. Co-authored-by: MrAlokTech <107493955+MrAlokTech@users.noreply.github.com> --- .jules/bolt.md | 4 ++++ script.js | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .jules/bolt.md 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(); }