diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..98cc448 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-24 - Avoid Object Serialization in DOM Attributes +**Learning:** During list rendering (`createPDFCard`), executing `JSON.stringify(object).replace(...)` to pass an entire object to an `onclick` inline handler creates massive performance overhead. Not only is serialization expensive, but it also generates huge string sizes in the DOM resulting in increased memory use. +**Action:** Instead of inline full-object serialization, pass a simple unique ID (`data-id="123"`) and rely on the event handler (`viewPDF(id)`) to look up the object reference in the data store (`pdfDatabase`). diff --git a/script.js b/script.js index bdc06f3..2b2a10f 100644 --- a/script.js +++ b/script.js @@ -788,7 +788,13 @@ function getEmbeddableUrl(url) { return `https://docs.google.com/gview?embedded=true&url=${encodeURIComponent(url)}`; } -async function viewPDF(pdf, pushToHistory = true) { +async function viewPDF(pdfOrId, pushToHistory = true) { + let pdf = pdfOrId; + if (typeof pdfOrId === 'string') { + pdf = pdfDatabase.find(p => p.id === pdfOrId); + } + if (!pdf) return; + const originalPdfPath = pdf.pdfUrl; logInteraction('view_pdf', pdf.title, pdf.id); @@ -1020,7 +1026,8 @@ function createPDFCard(pdf, favoritesList, index = 0, highlightRegex = null) { return safeText.replace(highlightRegex, '$1'); }; - const safePdfString = JSON.stringify(pdf).replace(/"/g, '"'); + // ⚡ Bolt Performance Optimization: Removed expensive JSON.stringify(pdf) which was running for every card. + // Now we just pass the PDF ID and let viewPDF handle the lookup. // --- NEW: Calculate Stagger Delay --- // Cap at 1s (20 items) so the list doesn't feel unresponsive @@ -1037,7 +1044,7 @@ function createPDFCard(pdf, favoritesList, index = 0, highlightRegex = null) {
${highlightText(pdf.description)}