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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 9 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
Expand Down