From 3ddb8698c7b92dc7b7a3220354bfe16b071e19ad Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 19 Oct 2026 15:35:48 +0000 Subject: [PATCH] perf(backtest): consolidate multiple reduce calls into single for loop Co-authored-by: toreleon <42534763+toreleon@users.noreply.github.com> --- src/agent/backtestRunner.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/agent/backtestRunner.ts b/src/agent/backtestRunner.ts index 2e878df..7315a7a 100644 --- a/src/agent/backtestRunner.ts +++ b/src/agent/backtestRunner.ts @@ -469,9 +469,17 @@ export async function runBacktestSession( runningPeak = Math.max(runningPeak, r.mtm_vnd); return Math.min(mn, r.mtm_vnd / runningPeak - 1); }, 0); - const totalCost = turnRows.reduce((s, r) => s + (r.cost_usd ?? 0), 0); - const totalIn = turnRows.reduce((s, r) => s + (r.in_tokens ?? 0), 0); - const totalOut = turnRows.reduce((s, r) => s + (r.out_tokens ?? 0), 0); + + // ⚡ Bolt: Consolidated multiple .reduce() passes into a single for loop to reduce iteration complexity from O(N*K) to O(N) + let totalCost = 0; + let totalIn = 0; + let totalOut = 0; + for (let i = 0; i < turnRows.length; i++) { + const r = turnRows[i]!; + totalCost += r.cost_usd ?? 0; + totalIn += r.in_tokens ?? 0; + totalOut += r.out_tokens ?? 0; + } const summary: SummaryPayload = { runId,