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(); }