diff --git a/.jules/bolt.md b/.jules/bolt.md index af3158d3..60c7fe89 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -7,3 +7,8 @@ **Learning:** Using `Object.entries(obj).find(([key]) => key === target)` creates O(N) array allocations for the entries and traverses them linearly just to do a simple property lookup. This adds unnecessary memory allocation overhead and Garbage Collection. **Action:** Use direct property lookup instead: `Object.prototype.hasOwnProperty.call(obj, target) ? obj[target as keyof typeof obj] : undefined`. This maintains O(1) performance while satisfying `security/detect-object-injection` linting rules. + +## 2025-02-12 — Array method overhead vs loops + +**Learning:** Chained array methods like `.filter().slice()` allocate intermediate arrays and execute the callback for every item in the array, even when fetching a tiny slice (like a limit of 5). +**Action:** Replace `.filter().slice(0, limit)` with an early-exit `for...of` loop with a `break` when querying a small number of items from a large list to prevent unnecessary iterations and allocations. diff --git a/app/layout.tsx b/app/layout.tsx index ba5653fd..5b99dfa6 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -17,7 +17,6 @@ import { Analytics } from "@vercel/analytics/react"; import { SpeedInsights } from "@vercel/speed-insights/next"; import type { Metadata } from "next"; import { Figtree, Space_Grotesk } from "next/font/google"; -import Script from "next/script"; const figtree = Figtree({ weight: ["300", "400", "500", "600", "700", "800", "900"], diff --git a/hooks/useTalks.ts b/hooks/useTalks.ts index 175be18b..5e261fbd 100644 --- a/hooks/useTalks.ts +++ b/hooks/useTalks.ts @@ -149,6 +149,15 @@ export const getTalkSpeakersWithDetails = async (year: string | number, speakerI export const getRelatedTalksByTrack = async (year: string | number, track: string, excludeTalkId: string, limit: number = 5): Promise => { const allTalks = await getAllTalks(year); - const sameTracks = allTalks.filter((t) => getTrackFromTalk(t) === track && t.id !== excludeTalkId); - return sameTracks.slice(0, limit); + if (limit <= 0) return []; + const result: Talk[] = []; + for (const t of allTalks) { + if (getTrackFromTalk(t) === track && t.id !== excludeTalkId) { + result.push(t); + if (result.length >= limit) { + break; + } + } + } + return result; };