From 1957734a876c8b27adc55e45317e5311104f2aec Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 1 Mar 2026 13:21:48 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Cache=20favorites=20to=20pr?= =?UTF-8?q?event=20redundant=20localStorage=20reads?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: MrAlokTech <107493955+MrAlokTech@users.noreply.github.com> --- .jules/bolt.md | 3 +++ script.js | 10 +++++++++- 2 files changed, 12 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..a16f732 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2025-02-14 - Redundant Synchronous localStorage Reads in Render Loop +**Learning:** Calling `localStorage.getItem` and `JSON.parse` inside high-frequency render loops (like `renderPDFs` triggered on input events) causes unnecessary synchronous I/O and garbage collection overhead, bottlenecking the main thread. +**Action:** Cache the results of expensive operations (like `localStorage` reads) in a memory variable (e.g., `favoritesCache`) and manually synchronize the cache when updates occur, completely skipping disk reads on subsequent renders. diff --git a/script.js b/script.js index bdc06f3..a484995 100644 --- a/script.js +++ b/script.js @@ -12,6 +12,7 @@ let searchTimeout; let adDatabase = {}; let isModalHistoryPushed = false; let db; // Defined globally, initialized later +let favoritesCache = null; // Cache for favorites // GAS const GAS_URL = "https://script.google.com/macros/s/AKfycby2lW5QdidC7o_JX0jlXa59uAjmmpFzOx-rye0N1x0r6hoYu-1CB65YrM1wPr7h-tZu/exec" @@ -1316,8 +1317,13 @@ async function handleCommentSubmit(e) { 10. EXTRAS (THEME, FAVORITES, EASTER EGGS) ========================================= */ function getFavorites() { + // ⚡ Bolt: Use a global cache (favoritesCache) to avoid synchronous localStorage.getItem and JSON.parse on every call during high-frequency render loops (e.g. renderPDFs inside search inputs) + 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 +1342,8 @@ function toggleFavorite(event, pdfId) { favorites.push(pdfId); showToast('Added to saved notes'); } + // ⚡ Bolt: Update the cache alongside localStorage to ensure the cache stays in sync + favoritesCache = favorites; localStorage.setItem('classNotesFavorites', JSON.stringify(favorites)); renderPDFs(); }