From cf3d32b745dd1d9b5720f279a36bd437edf13557 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 22 May 2026 01:38:53 +0000 Subject: [PATCH] perf: Optimize chained array operations in CSV export rendering Replaced `.map().join()` and `.filter().map().join()` chains with single-pass `for...of` loops in `src/csv.ts`. This avoids unnecessary array allocations and reduces Garbage Collection pressure on the hot rendering path when generating datasets. Co-authored-by: schmug <38227427+schmug@users.noreply.github.com> --- src/csv.ts | 31 +++++++++++++++++++++++-------- test/mcp.test.ts | 2 +- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/csv.ts b/src/csv.ts index f16257f..fc832ad 100644 --- a/src/csv.ts +++ b/src/csv.ts @@ -41,8 +41,15 @@ export function escapeCsvField(value: string): string { function formatFindings( validations: Array<{ status: string; message: string }>, ): string { + // ⚡ Bolt Optimization: Use for...of loop instead of .map().join("") + // Avoids intermediate array allocations for the hot rendering path. if (validations.length === 0) return ""; - return validations.map((v) => `[${v.status}] ${v.message}`).join("; "); + let result = ""; + for (const v of validations) { + if (result.length > 0) result += "; "; + result += `[${v.status}] ${v.message}`; + } + return result; } function dkimRawSummary( @@ -79,16 +86,24 @@ export function generateCsv(result: ScanResult): string { for (const key of protocols) { const proto = result.protocols[key]; - const recs = result.breakdown.recommendations - .filter((r) => r.protocol === key) - .map((r) => `[P${r.priority}] ${r.title}`) - .join("; "); + + // ⚡ Bolt Optimization: Use a single-pass loop instead of .filter().map().join("") + let recs = ""; + for (const r of result.breakdown.recommendations) { + if (r.protocol === key) { + if (recs.length > 0) recs += "; "; + recs += `[P${r.priority}] ${r.title}`; + } + } let rawRecord: string; if (key === "mx") { - rawRecord = result.protocols.mx.records - .map((r) => `${r.priority} ${r.exchange}`) - .join("; "); + // ⚡ Bolt Optimization: Use a single-pass loop instead of .map().join("") + rawRecord = ""; + for (const r of result.protocols.mx.records) { + if (rawRecord.length > 0) rawRecord += "; "; + rawRecord += `${r.priority} ${r.exchange}`; + } } else if (key === "dkim") { rawRecord = dkimRawSummary(result.protocols.dkim.selectors); } else if (key === "mta_sts") { diff --git a/test/mcp.test.ts b/test/mcp.test.ts index 25f6ad7..7f21296 100644 --- a/test/mcp.test.ts +++ b/test/mcp.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { handleMcpRequest, MCP_PROTOCOL_VERSION,