From ee3c0e7d4f8888597eaa4a97e78cbc638b955a5a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 24 Mar 2026 13:11:22 +0000 Subject: [PATCH] feat: optimize search filtering with early returns and pre-calculated index - Added `prepareSearchIndex` to pre-calculate search strings and formats when data is loaded. - Refactored `renderPDFs` filter loop to use explicit early returns instead of eager boolean evaluation. - Pre-calculations prevent redundant string allocations and iteration logic on every keystroke. Co-authored-by: MrAlokTech <107493955+MrAlokTech@users.noreply.github.com> --- .jules/bolt.md | 3 +++ script.js | 57 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..2f77f4a --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-24 - Eager Evaluation Defeats Short-Circuiting in Array Filters +**Learning:** JavaScript short-circuiting in `return a && b && c` is useless if you eagerly evaluate and assign the boolean variables *before* the return statement. In `renderPDFs()`, expensive string lowercasing and array inclusions (`matchesSearch`) were executing for *every* item in the database on *every* keystroke, regardless of whether the item belonged to the current class or semester, because the booleans were calculated upfront. +**Action:** Use explicit early returns (`if (!condition) return false;`) in filter loops instead of assigning variables to group boolean expressions, and pre-calculate derived search properties (`_searchStr`) during the initial data load to avoid per-render recalculations. \ No newline at end of file diff --git a/script.js b/script.js index 22f399d..8af395b 100644 --- a/script.js +++ b/script.js @@ -416,6 +416,30 @@ async function syncClassSwitcher() { renderSemesterTabs(); } + +function prepareSearchIndex(data) { + const now = new Date(); + data.forEach(pdf => { + // Pre-calculate search string + pdf._searchStr = `${pdf.title || ''} ${pdf.description || ''} ${pdf.category || ''} ${pdf.author || ''}`.toLowerCase(); + + // Handle Date parsing (Firestore Timestamp or ISO string) + let uploadDateObj; + if (pdf.uploadDate && typeof pdf.uploadDate.toDate === 'function') { + uploadDateObj = pdf.uploadDate.toDate(); + } else { + uploadDateObj = new Date(pdf.uploadDate); + } + + pdf._formattedDate = uploadDateObj.toLocaleDateString('en-US', { + year: 'numeric', month: 'short', day: 'numeric' + }); + + const timeDiff = now - uploadDateObj; + pdf._isNew = timeDiff < (7 * 24 * 60 * 60 * 1000); // 7 days + }); +} + async function loadPDFDatabase() { if (isMaintenanceActive) return; @@ -454,6 +478,7 @@ async function loadPDFDatabase() { if (shouldUseCache) { pdfDatabase = cachedData; + prepareSearchIndex(pdfDatabase); // --- FIX: CALL THIS TO POPULATE UI --- syncClassSwitcher(); renderSemesterTabs(); @@ -477,6 +502,8 @@ async function loadPDFDatabase() { data: pdfDatabase })); + prepareSearchIndex(pdfDatabase); + // --- FIX: CALL THIS TO POPULATE UI --- syncClassSwitcher(); renderPDFs(); @@ -905,26 +932,20 @@ function renderPDFs() { // Locate renderPDFs() in script.js and update the filter section const filteredPdfs = pdfDatabase.filter(pdf => { - const matchesSemester = pdf.semester === currentSemester; - - // NEW: Check if the PDF class matches the UI's current class selection - // Note: If old documents don't have this field, they will be hidden. - const matchesClass = pdf.class === currentClass; + if (pdf.class !== currentClass) return false; + if (pdf.semester !== currentSemester) return false; - let matchesCategory = false; if (currentCategory === 'favorites') { - matchesCategory = favorites.includes(pdf.id); - } else { - matchesCategory = currentCategory === 'all' || pdf.category === currentCategory; + if (!favorites.includes(pdf.id)) return false; + } else if (currentCategory !== 'all') { + if (pdf.category !== currentCategory) return false; } - const matchesSearch = pdf.title.toLowerCase().includes(searchTerm) || - pdf.description.toLowerCase().includes(searchTerm) || - pdf.category.toLowerCase().includes(searchTerm) || - pdf.author.toLowerCase().includes(searchTerm); + if (searchTerm && pdf._searchStr) { + if (!pdf._searchStr.includes(searchTerm)) return false; + } - // Update return statement to include matchesClass - return matchesSemester && matchesClass && matchesCategory && matchesSearch; + return true; }); updatePDFCount(filteredPdfs.length); @@ -994,9 +1015,7 @@ function createPDFCard(pdf, favoritesList, index = 0, highlightRegex = null) { const heartIconClass = isFav ? 'fas' : 'far'; const btnActiveClass = isFav ? 'active' : ''; - const uploadDateObj = new Date(pdf.uploadDate); - const timeDiff = new Date() - uploadDateObj; - const isNew = timeDiff < (7 * 24 * 60 * 60 * 1000); // 7 days + const isNew = pdf._isNew !== undefined ? pdf._isNew : (new Date() - new Date(pdf.uploadDate)) < (7 * 24 * 60 * 60 * 1000); const newBadgeHTML = isNew ? `NEW` @@ -1011,7 +1030,7 @@ function createPDFCard(pdf, favoritesList, index = 0, highlightRegex = null) { const categoryIcon = categoryIcons[pdf.category] || 'fa-file-pdf'; // Formatting Date - const formattedDate = new Date(pdf.uploadDate).toLocaleDateString('en-US', { + const formattedDate = pdf._formattedDate || new Date(pdf.uploadDate).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });