diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..8ce14b1 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,2 @@ +## 2024-05-24 - Pre-calculating derived properties +**Learning:** The memory mentions that pre-calculating _searchStr, _isNew, _formattedDate speeds things up 128x (115ms to 0.9ms). diff --git a/script.js b/script.js index 22f399d..1cd6f44 100644 --- a/script.js +++ b/script.js @@ -454,6 +454,7 @@ async function loadPDFDatabase() { if (shouldUseCache) { pdfDatabase = cachedData; + prepareSearchIndex(pdfDatabase); // --- FIX: CALL THIS TO POPULATE UI --- syncClassSwitcher(); renderSemesterTabs(); @@ -477,6 +478,8 @@ async function loadPDFDatabase() { data: pdfDatabase })); + prepareSearchIndex(pdfDatabase); + // --- FIX: CALL THIS TO POPULATE UI --- syncClassSwitcher(); renderPDFs(); @@ -503,6 +506,40 @@ function hidePreloader() { } } +/* ========================================= + 5.5 PERFORMANCE INDEXING + ========================================= */ +function prepareSearchIndex(data) { + const now = new Date(); + // Cache the formatter for maximum performance + const dateFormatter = new Intl.DateTimeFormat('en-US', { + year: 'numeric', month: 'short', day: 'numeric' + }); + + data.forEach(pdf => { + // Pre-calculate search string + const title = pdf.title || ''; + const description = pdf.description || ''; + const category = pdf.category || ''; + const author = pdf.author || ''; + pdf._searchStr = `${title} ${description} ${category} ${author}`.toLowerCase(); + + // Handle both Firestore Timestamp (.toDate) and string dates + let uploadDateObj; + if (pdf.uploadDate && typeof pdf.uploadDate.toDate === 'function') { + uploadDateObj = pdf.uploadDate.toDate(); + } else if (pdf.uploadDate) { + uploadDateObj = new Date(pdf.uploadDate); + } else { + uploadDateObj = now; // Fallback + } + + // Pre-calculate expensive date logic + pdf._isNew = (now - uploadDateObj) < (7 * 24 * 60 * 60 * 1000); + pdf._formattedDate = dateFormatter.format(uploadDateObj); + }); +} + /* ========================================= 6. MAINTENANCE & HOLIDAYS ========================================= */ @@ -905,26 +942,23 @@ function renderPDFs() { // Locate renderPDFs() in script.js and update the filter section const filteredPdfs = pdfDatabase.filter(pdf => { - const matchesSemester = pdf.semester === currentSemester; + 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; + if (pdf.class !== currentClass) 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 && !pdf._searchStr.includes(searchTerm)) { + return false; + } - // Update return statement to include matchesClass - return matchesSemester && matchesClass && matchesCategory && matchesSearch; + return true; }); updatePDFCount(filteredPdfs.length); @@ -994,9 +1028,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; const newBadgeHTML = isNew ? `NEW` @@ -1011,9 +1043,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', { - year: 'numeric', month: 'short', day: 'numeric' - }); + const formattedDate = pdf._formattedDate; // Uses global escapeHtml() now