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-03-07 - [renderPDFs Filter Optimization]
**Learning:** In client-side filtering (like in Single Page Apps filtering large arrays without a framework), the order of conditions in array.filter matters heavily. String operations (.toLowerCase(), .includes()) are extremely expensive compared to basic equality checks (===) on primitive values. Concatenating strings over and over is also costly.
**Action:** Always place cheap strict equality checks first as early returns to prune the dataset before performing expensive string searching. Additionally, calculate and cache compound search strings on the objects lazily so they are only computed once per item, enabling rapid single-pass matching for subsequent searches.
28 changes: 13 additions & 15 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,26 +902,24 @@ function renderPDFs() {

// Locate renderPDFs() in script.js and update the filter section
const filteredPdfs = pdfDatabase.filter(pdf => {
const matchesSemester = pdf.semester === currentSemester;
// ⚑ Bolt: Early returns for cheap equality checks to skip expensive string operations
if (pdf.semester !== currentSemester) return false;
if (pdf.class !== currentClass) 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' && 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) return true;

// ⚑ Bolt: Lazily calculate and cache search string for efficient single-pass matching
if (!pdf._searchStr) {
pdf._searchStr = `${pdf.title || ''} ${pdf.description || ''} ${pdf.category || ''} ${pdf.author || ''}`.toLowerCase();
}

// Update return statement to include matchesClass
return matchesSemester && matchesClass && matchesCategory && matchesSearch;
return pdf._searchStr.includes(searchTerm);
});

updatePDFCount(filteredPdfs.length);
Expand Down