From 7723b0639cfefd25c06d32099bffa0c914eb673a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 23 Mar 2026 03:57:47 +0000 Subject: [PATCH] bolt: replace O(N^2) Array.includes with O(N) Set.has in filter loops Co-authored-by: xb1g <70068561+xb1g@users.noreply.github.com> --- app/api/assignments/[id]/nodes/route.ts | 4 +++- app/api/classrooms/[id]/grading/route.ts | 4 +++- components/teams/StudentTeamDashboard.tsx | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/api/assignments/[id]/nodes/route.ts b/app/api/assignments/[id]/nodes/route.ts index f522da40..6c83a1d0 100644 --- a/app/api/assignments/[id]/nodes/route.ts +++ b/app/api/assignments/[id]/nodes/route.ts @@ -242,7 +242,9 @@ export async function POST( } const existingNodeIds = existingNodes?.map((n) => n.id) || []; - const missingNodes = node_ids.filter((id) => !existingNodeIds.includes(id)); + // Optimization: Use Set for O(1) lookups instead of Array.includes which is O(N) + const existingNodeIdsSet = new Set(existingNodeIds); + const missingNodes = node_ids.filter((id) => !existingNodeIdsSet.has(id)); if (missingNodes.length > 0) { return NextResponse.json( diff --git a/app/api/classrooms/[id]/grading/route.ts b/app/api/classrooms/[id]/grading/route.ts index 09bffc1a..2f054dbb 100644 --- a/app/api/classrooms/[id]/grading/route.ts +++ b/app/api/classrooms/[id]/grading/route.ts @@ -262,7 +262,9 @@ export async function GET( // Get additional profiles for members not in submissions const classroomUserIds = classroomMembers.map(m => m.user_id); - const missingUserIds = classroomUserIds.filter(id => !submissionUserIds.includes(id)); + // Optimization: Use Set for O(1) lookups instead of Array.includes which is O(N) + const submissionUserIdsSet = new Set(submissionUserIds); + const missingUserIds = classroomUserIds.filter(id => !submissionUserIdsSet.has(id)); let additionalProfiles: any[] = []; if (missingUserIds.length > 0) { diff --git a/components/teams/StudentTeamDashboard.tsx b/components/teams/StudentTeamDashboard.tsx index d5f1e30f..1f52bf10 100644 --- a/components/teams/StudentTeamDashboard.tsx +++ b/components/teams/StudentTeamDashboard.tsx @@ -93,7 +93,9 @@ export function StudentTeamDashboard({ // Find available teams (teams user is NOT in) const userTeamIds = userTeams.map((t) => t.id); - const availableTeams = allTeams.filter((t) => !userTeamIds.includes(t.id)); + // Optimization: Use Set for O(1) lookups instead of Array.includes which is O(N) + const userTeamIdsSet = new Set(userTeamIds); + const availableTeams = allTeams.filter((t) => !userTeamIdsSet.has(t.id)); // Get teams with open spots const teamsWithOpenSpots = availableTeams.filter(