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 @@
## 2024-05-18 - [Optimized render loop strings and dates]
**Learning:** Instantiating `Intl.DateTimeFormat` and parsing dates in `createPDFCard` on every render creates massive JS heap overhead on large sets, blocking typing and scrolling.
**Action:** Created `prepareSearchIndex(data)` that runs exactly once when `pdfDatabase` is populated, assigning private variables `_searchStr`, `_isNew`, and `_formattedDate`. `renderPDFs` now falls back to standard rendering only if properties don't exist.
57 changes: 47 additions & 10 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ async function loadPDFDatabase() {

if (shouldUseCache) {
pdfDatabase = cachedData;
prepareSearchIndex(pdfDatabase);
// --- FIX: CALL THIS TO POPULATE UI ---
syncClassSwitcher();
renderSemesterTabs();
Expand All @@ -513,6 +514,7 @@ async function loadPDFDatabase() {
data: pdfDatabase
}));

prepareSearchIndex(pdfDatabase);
// --- FIX: CALL THIS TO POPULATE UI ---
syncClassSwitcher();
renderPDFs();
Expand Down Expand Up @@ -961,10 +963,15 @@ function renderPDFs() {
matchesCategory = currentCategory === 'all' || pdf.category === currentCategory;
}

const matchesSearch = pdf.title.toLowerCase().includes(searchTerm) ||
pdf.description.toLowerCase().includes(searchTerm) ||
pdf.category.toLowerCase().includes(searchTerm) ||
pdf.author.toLowerCase().includes(searchTerm);
let matchesSearch = false;
if (pdf._searchStr) {
matchesSearch = pdf._searchStr.includes(searchTerm);
} else {
matchesSearch = pdf.title.toLowerCase().includes(searchTerm) ||
pdf.description.toLowerCase().includes(searchTerm) ||
pdf.category.toLowerCase().includes(searchTerm) ||
pdf.author.toLowerCase().includes(searchTerm);
}

// Update return statement to include matchesClass
return matchesSemester && matchesClass && matchesCategory && matchesSearch;
Expand Down Expand Up @@ -1037,9 +1044,12 @@ 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
let isNew = pdf._isNew;
if (isNew === undefined) {
const uploadDateObj = new Date(pdf.uploadDate);
const timeDiff = new Date() - uploadDateObj;
isNew = timeDiff < (7 * 24 * 60 * 60 * 1000); // 7 days
}

const newBadgeHTML = isNew
? `<span style="background:var(--error-color); color:white; font-size:0.6rem; padding:2px 6px; border-radius:4px; margin-left:8px; vertical-align:middle;">NEW</span>`
Expand All @@ -1054,9 +1064,12 @@ 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'
});
let formattedDate = pdf._formattedDate;
if (formattedDate === undefined) {
formattedDate = new Date(pdf.uploadDate).toLocaleDateString('en-US', {
year: 'numeric', month: 'short', day: 'numeric'
});
}

// Uses global escapeHtml() now

Expand Down Expand Up @@ -1718,6 +1731,30 @@ function triggerPrank(overlay, textEl, barEl, closeBtn) {
});
}

/* =========================================
PERFORMANCE HELPERS
========================================= */

function prepareSearchIndex(data) {
const now = Date.now();
const formatter = new Intl.DateTimeFormat('en-US', {
year: 'numeric', month: 'short', day: 'numeric'
});

data.forEach(item => {
item._searchStr = [
item.title,
item.description,
item.category,
item.author
].join(' ').toLowerCase();

const uploadTimestamp = new Date(item.uploadDate).getTime();
item._isNew = !isNaN(uploadTimestamp) ? (now - uploadTimestamp) < (7 * 24 * 60 * 60 * 1000) : false;
item._formattedDate = !isNaN(uploadTimestamp) ? formatter.format(uploadTimestamp) : 'Unknown Date';
});
}

/* =========================================
NEW YEAR COUNTDOWN
========================================= */
Expand Down