diff --git a/.jules/bolt.md b/.jules/bolt.md index 57daf471..3f822941 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -3,3 +3,6 @@ **Learning:** `Date.parse(value)` returns the timestamp primitive directly, while `new Date(value).getTime()` also constructs a `Date` object. Both use the same ECMAScript string-parsing semantics for these call sites. **Action:** In frequently executed paths that only need a timestamp primitive, prefer `Date.parse(value)`. Treat the allocation reduction as a bounded micro-optimization unless a committed benchmark establishes a larger runtime effect. +## 2023-10-12 - [Array Iteration Optimization in Session Export] +**Learning:** Chained `.map(...).filter(...)` operations on large arrays allocate an intermediate array which creates GC overhead. This is problematic on hot paths like dashboard data endpoints. +**Action:** Replace `.map().filter()` chains with a single `.reduce()` pass for array aggregations to minimize allocations and GC pause times. diff --git a/packages/web/src/app/api/orgs/[orgSlug]/dashboard/sessions/route.ts b/packages/web/src/app/api/orgs/[orgSlug]/dashboard/sessions/route.ts index 7d6a4d4a..2e304acb 100644 --- a/packages/web/src/app/api/orgs/[orgSlug]/dashboard/sessions/route.ts +++ b/packages/web/src/app/api/orgs/[orgSlug]/dashboard/sessions/route.ts @@ -196,9 +196,11 @@ export async function GET( include: sessionInclude, }) const byId = new Map(rows.map((s) => [s.id, s])) - csvSessions = ids - .map((id) => byId.get(id)) - .filter((s): s is SessionWithInclude => !!s) + csvSessions = ids.reduce((acc, id) => { + const s = byId.get(id) + if (s) acc.push(s) + return acc + }, [] as SessionWithInclude[]) } } else { csvSessions = await db.claudeSession.findMany({ @@ -245,9 +247,11 @@ export async function GET( include: sessionInclude, }) const byId = new Map(rows.map((s) => [s.id, s])) - sessions = ids - .map((id) => byId.get(id)) - .filter((s): s is SessionWithInclude => !!s) + sessions = ids.reduce((acc, id) => { + const s = byId.get(id) + if (s) acc.push(s) + return acc + }, [] as SessionWithInclude[]) } } else { const [rows, countResult] = await Promise.all([