diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..693d89c --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2025-05-18 - [Pre-calculating derived properties] +**Learning:** Moving date parsing, string concatenation, and lowercasing out of the render loop (which runs on every keystroke) into a one-time data load step significantly reduces CPU overhead and avoids unnecessary garbage collection during search filtering. +**Action:** Use a `prepareSearchIndex` function to map over incoming data to add runtime properties like `_searchStr`, `_formattedDate`, and `_isNew`. diff --git a/script.js b/script.js index bdc06f3..596e5d9 100644 --- a/script.js +++ b/script.js @@ -335,6 +335,27 @@ function getAdData(slotName) { /* ========================================= 5. DATA LOADING WITH CACHING ========================================= */ +function prepareSearchIndex(pdf) { + // Check for Firestore Timestamps (via .toDate()) before fallback to standard Date parsing + const uploadDateObj = pdf.uploadDate && typeof pdf.uploadDate.toDate === 'function' + ? pdf.uploadDate.toDate() + : new Date(pdf.uploadDate); + + // _searchStr is a lowercased concatenation of searchable fields + pdf._searchStr = `${pdf.title} ${pdf.description} ${pdf.category} ${pdf.author}`.toLowerCase(); + + // Formatting Date once + pdf._formattedDate = uploadDateObj.toLocaleDateString('en-US', { + year: 'numeric', month: 'short', day: 'numeric' + }); + + // Calculate if it's new once + const timeDiff = new Date() - uploadDateObj; + pdf._isNew = timeDiff < (7 * 24 * 60 * 60 * 1000); // 7 days + + return pdf; +} + function renderSemesterTabs() { const container = document.getElementById('semesterTabsContainer'); if (!container) return; @@ -450,7 +471,7 @@ async function loadPDFDatabase() { } if (shouldUseCache) { - pdfDatabase = cachedData; + pdfDatabase = cachedData.map(prepareSearchIndex); // --- FIX: CALL THIS TO POPULATE UI --- syncClassSwitcher(); renderSemesterTabs(); @@ -469,6 +490,9 @@ async function loadPDFDatabase() { pdfDatabase.push({ id: doc.id, ...doc.data() }); }); + // Mapping to calculate _searchStr before saving to local storage so its already computed + pdfDatabase = pdfDatabase.map(prepareSearchIndex); + localStorage.setItem(CACHE_KEY, JSON.stringify({ timestamp: new Date().getTime(), data: pdfDatabase @@ -902,26 +926,22 @@ function renderPDFs() { // Locate renderPDFs() in script.js and update the filter section const filteredPdfs = pdfDatabase.filter(pdf => { - const matchesSemester = pdf.semester === currentSemester; + // EARLY RETURNS: Check cheapest equality matches first + if (pdf.class !== currentClass) return false; + if (pdf.semester !== currentSemester) return false; - // 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; - - 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); + // Fast search using pre-calculated _searchStr + if (searchTerm && !pdf._searchStr.includes(searchTerm)) { + return false; + } - // Update return statement to include matchesClass - return matchesSemester && matchesClass && matchesCategory && matchesSearch; + return true; }); updatePDFCount(filteredPdfs.length); @@ -991,11 +1011,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 newBadgeHTML = isNew + const newBadgeHTML = pdf._isNew ? `NEW` : ''; @@ -1007,11 +1023,6 @@ 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', { - year: 'numeric', month: 'short', day: 'numeric' - }); - // Uses global escapeHtml() now const highlightText = (text) => { @@ -1033,7 +1044,7 @@ function createPDFCard(pdf, favoritesList, index = 0, highlightRegex = null) {
${highlightText(pdf.description)}