From b7d5245d389df9d82ab3f613a7e3c4b7be44353b Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 16 Sep 2026 18:17:42 +0000 Subject: [PATCH] refactor(dossier): optimize string concatenation in markdown generation Replaced string concatenation (`+=`) inside `forEach` loops with `Array.map().join('')` in `compileSingleAuditToMarkdown` and `compileDossierToMarkdown`. This optimizes memory allocation and execution time by avoiding the creation of numerous intermediate string objects during loop iterations, particularly for large arrays of findings or dossier items. Both ES Module (`.js`) and CommonJS (`.cjs`) versions were updated and kept in sync. --- extension/shared/dossier-compiler.cjs | 59 ++++++++++++++------------- extension/shared/dossier-compiler.js | 59 ++++++++++++++------------- 2 files changed, 62 insertions(+), 56 deletions(-) diff --git a/extension/shared/dossier-compiler.cjs b/extension/shared/dossier-compiler.cjs index 1ded244..f69eed9 100644 --- a/extension/shared/dossier-compiler.cjs +++ b/extension/shared/dossier-compiler.cjs @@ -19,13 +19,13 @@ function compileSingleAuditToMarkdown(item) { if (Array.isArray(result.findings) && result.findings.length > 0) { md += `## Evidence-Based Findings & Recommendations\n\n`; - result.findings.forEach((f, idx) => { - md += `### ${idx + 1}. [${f.tier || 'T1'}] ${f.citation || 'Citation'}\n\n`; - md += `- **Severity:** \`${(f.severity || 'info').toUpperCase()}\`\n`; - md += `- **Observation:** ${f.observation || ''}\n`; - md += `- **Empirical Evidence:** ${f.evidence || ''}\n`; - md += `- **Actionable Recommendation:** ${f.recommendation || ''}\n\n`; - }); + md += result.findings.map((f, idx) => + `### ${idx + 1}. [${f.tier || 'T1'}] ${f.citation || 'Citation'}\n\n` + + `- **Severity:** \`${(f.severity || 'info').toUpperCase()}\`\n` + + `- **Observation:** ${f.observation || ''}\n` + + `- **Empirical Evidence:** ${f.evidence || ''}\n` + + `- **Actionable Recommendation:** ${f.recommendation || ''}\n\n` + ).join(''); } if (result.improvedDraft && result.improvedDraft.content) { @@ -52,39 +52,42 @@ function compileDossierToMarkdown(dossierItems, courseTitle = 'Canvas Course') { md += `## Table of Audited Materials\n\n`; md += `| # | Component Title | Type | Bloom's Demand | Alignment Score |\n`; md += `| :--- | :--- | :--- | :--- | :--- |\n`; - dossierItems.forEach((item, idx) => { + + md += dossierItems.map((item, idx) => { const s = item.result?.summary || {}; - md += `| ${idx + 1} | ${item.title || 'Untitled'} | ${item.pageType || 'Page'} | ${s.bloomsLevel || 'N/A'} | ${s.alignmentScore || 'N/A'} |\n`; - }); + return `| ${idx + 1} | ${item.title || 'Untitled'} | ${item.pageType || 'Page'} | ${s.bloomsLevel || 'N/A'} | ${s.alignmentScore || 'N/A'} |\n`; + }).join(''); + md += `\n---\n\n`; - dossierItems.forEach((item, idx) => { - md += `## Section ${idx + 1}: ${item.title || 'Component'}\n\n`; - md += `> **Type:** ${item.pageType || 'Page'} | **Source:** ${item.url || 'N/A'}\n\n`; + md += dossierItems.map((item, idx) => { + let sectionMd = `## Section ${idx + 1}: ${item.title || 'Component'}\n\n`; + sectionMd += `> **Type:** ${item.pageType || 'Page'} | **Source:** ${item.url || 'N/A'}\n\n`; const r = item.result || {}; - md += `### Summary\n`; - md += `- **Cognitive Demand:** ${r.summary?.bloomsLevel || 'N/A'}\n`; - md += `- **Alignment Rating:** ${r.summary?.alignmentScore || 'N/A'}\n`; - md += `- **Takeaway:** ${r.summary?.keyTakeaway || 'N/A'}\n\n`; + sectionMd += `### Summary\n`; + sectionMd += `- **Cognitive Demand:** ${r.summary?.bloomsLevel || 'N/A'}\n`; + sectionMd += `- **Alignment Rating:** ${r.summary?.alignmentScore || 'N/A'}\n`; + sectionMd += `- **Takeaway:** ${r.summary?.keyTakeaway || 'N/A'}\n\n`; if (Array.isArray(r.findings) && r.findings.length > 0) { - md += `### Findings & Evidence\n\n`; - r.findings.forEach((f, fIdx) => { - md += `#### ${idx + 1}.${fIdx + 1} [${f.tier || 'T1'}] ${f.citation || 'Citation'}\n`; - md += `- **Observation:** ${f.observation || ''}\n`; - md += `- **Evidence:** ${f.evidence || ''}\n`; - md += `- **Recommendation:** ${f.recommendation || ''}\n\n`; - }); + sectionMd += `### Findings & Evidence\n\n`; + sectionMd += r.findings.map((f, fIdx) => + `#### ${idx + 1}.${fIdx + 1} [${f.tier || 'T1'}] ${f.citation || 'Citation'}\n` + + `- **Observation:** ${f.observation || ''}\n` + + `- **Evidence:** ${f.evidence || ''}\n` + + `- **Recommendation:** ${f.recommendation || ''}\n\n` + ).join(''); } if (r.improvedDraft && r.improvedDraft.content) { - md += `### ${r.improvedDraft.title || 'Revised Draft'}\n\n`; - md += `${r.improvedDraft.content}\n\n`; + sectionMd += `### ${r.improvedDraft.title || 'Revised Draft'}\n\n`; + sectionMd += `${r.improvedDraft.content}\n\n`; } - md += `---\n\n`; - }); + sectionMd += `---\n\n`; + return sectionMd; + }).join(''); md += `*Generated by [idstack](https://github.com/savvides/idstack) — Evidence-Based Course Design.*`; return md; diff --git a/extension/shared/dossier-compiler.js b/extension/shared/dossier-compiler.js index eec4742..92c6aa5 100644 --- a/extension/shared/dossier-compiler.js +++ b/extension/shared/dossier-compiler.js @@ -19,13 +19,13 @@ export function compileSingleAuditToMarkdown(item) { if (Array.isArray(result.findings) && result.findings.length > 0) { md += `## Evidence-Based Findings & Recommendations\n\n`; - result.findings.forEach((f, idx) => { - md += `### ${idx + 1}. [${f.tier || 'T1'}] ${f.citation || 'Citation'}\n\n`; - md += `- **Severity:** \`${(f.severity || 'info').toUpperCase()}\`\n`; - md += `- **Observation:** ${f.observation || ''}\n`; - md += `- **Empirical Evidence:** ${f.evidence || ''}\n`; - md += `- **Actionable Recommendation:** ${f.recommendation || ''}\n\n`; - }); + md += result.findings.map((f, idx) => + `### ${idx + 1}. [${f.tier || 'T1'}] ${f.citation || 'Citation'}\n\n` + + `- **Severity:** \`${(f.severity || 'info').toUpperCase()}\`\n` + + `- **Observation:** ${f.observation || ''}\n` + + `- **Empirical Evidence:** ${f.evidence || ''}\n` + + `- **Actionable Recommendation:** ${f.recommendation || ''}\n\n` + ).join(''); } if (result.improvedDraft && result.improvedDraft.content) { @@ -52,39 +52,42 @@ export function compileDossierToMarkdown(dossierItems, courseTitle = 'Canvas Cou md += `## Table of Audited Materials\n\n`; md += `| # | Component Title | Type | Bloom's Demand | Alignment Score |\n`; md += `| :--- | :--- | :--- | :--- | :--- |\n`; - dossierItems.forEach((item, idx) => { + + md += dossierItems.map((item, idx) => { const s = item.result?.summary || {}; - md += `| ${idx + 1} | ${item.title || 'Untitled'} | ${item.pageType || 'Page'} | ${s.bloomsLevel || 'N/A'} | ${s.alignmentScore || 'N/A'} |\n`; - }); + return `| ${idx + 1} | ${item.title || 'Untitled'} | ${item.pageType || 'Page'} | ${s.bloomsLevel || 'N/A'} | ${s.alignmentScore || 'N/A'} |\n`; + }).join(''); + md += `\n---\n\n`; - dossierItems.forEach((item, idx) => { - md += `## Section ${idx + 1}: ${item.title || 'Component'}\n\n`; - md += `> **Type:** ${item.pageType || 'Page'} | **Source:** ${item.url || 'N/A'}\n\n`; + md += dossierItems.map((item, idx) => { + let sectionMd = `## Section ${idx + 1}: ${item.title || 'Component'}\n\n`; + sectionMd += `> **Type:** ${item.pageType || 'Page'} | **Source:** ${item.url || 'N/A'}\n\n`; const r = item.result || {}; - md += `### Summary\n`; - md += `- **Cognitive Demand:** ${r.summary?.bloomsLevel || 'N/A'}\n`; - md += `- **Alignment Rating:** ${r.summary?.alignmentScore || 'N/A'}\n`; - md += `- **Takeaway:** ${r.summary?.keyTakeaway || 'N/A'}\n\n`; + sectionMd += `### Summary\n`; + sectionMd += `- **Cognitive Demand:** ${r.summary?.bloomsLevel || 'N/A'}\n`; + sectionMd += `- **Alignment Rating:** ${r.summary?.alignmentScore || 'N/A'}\n`; + sectionMd += `- **Takeaway:** ${r.summary?.keyTakeaway || 'N/A'}\n\n`; if (Array.isArray(r.findings) && r.findings.length > 0) { - md += `### Findings & Evidence\n\n`; - r.findings.forEach((f, fIdx) => { - md += `#### ${idx + 1}.${fIdx + 1} [${f.tier || 'T1'}] ${f.citation || 'Citation'}\n`; - md += `- **Observation:** ${f.observation || ''}\n`; - md += `- **Evidence:** ${f.evidence || ''}\n`; - md += `- **Recommendation:** ${f.recommendation || ''}\n\n`; - }); + sectionMd += `### Findings & Evidence\n\n`; + sectionMd += r.findings.map((f, fIdx) => + `#### ${idx + 1}.${fIdx + 1} [${f.tier || 'T1'}] ${f.citation || 'Citation'}\n` + + `- **Observation:** ${f.observation || ''}\n` + + `- **Evidence:** ${f.evidence || ''}\n` + + `- **Recommendation:** ${f.recommendation || ''}\n\n` + ).join(''); } if (r.improvedDraft && r.improvedDraft.content) { - md += `### ${r.improvedDraft.title || 'Revised Draft'}\n\n`; - md += `${r.improvedDraft.content}\n\n`; + sectionMd += `### ${r.improvedDraft.title || 'Revised Draft'}\n\n`; + sectionMd += `${r.improvedDraft.content}\n\n`; } - md += `---\n\n`; - }); + sectionMd += `---\n\n`; + return sectionMd; + }).join(''); md += `*Generated by [idstack](https://github.com/savvides/idstack) — Evidence-Based Course Design.*`; return md;