Skip to content

Commit 54a7112

Browse files
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>
1 parent 34f9d08 commit 54a7112

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

app/api/assignments/[id]/nodes/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ export async function POST(
242242
}
243243

244244
const existingNodeIds = existingNodes?.map((n) => n.id) || [];
245-
const missingNodes = node_ids.filter((id) => !existingNodeIds.includes(id));
245+
// Optimization: Use Set for O(1) lookups instead of Array.includes which is O(N)
246+
const existingNodeIdsSet = new Set(existingNodeIds);
247+
const missingNodes = node_ids.filter((id) => !existingNodeIdsSet.has(id));
246248

247249
if (missingNodes.length > 0) {
248250
return NextResponse.json(

app/api/classrooms/[id]/grading/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ export async function GET(
262262

263263
// Get additional profiles for members not in submissions
264264
const classroomUserIds = classroomMembers.map(m => m.user_id);
265-
const missingUserIds = classroomUserIds.filter(id => !submissionUserIds.includes(id));
265+
// Optimization: Use Set for O(1) lookups instead of Array.includes which is O(N)
266+
const submissionUserIdsSet = new Set(submissionUserIds);
267+
const missingUserIds = classroomUserIds.filter(id => !submissionUserIdsSet.has(id));
266268

267269
let additionalProfiles: any[] = [];
268270
if (missingUserIds.length > 0) {

components/teams/StudentTeamDashboard.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ export function StudentTeamDashboard({
9393

9494
// Find available teams (teams user is NOT in)
9595
const userTeamIds = userTeams.map((t) => t.id);
96-
const availableTeams = allTeams.filter((t) => !userTeamIds.includes(t.id));
96+
// Optimization: Use Set for O(1) lookups instead of Array.includes which is O(N)
97+
const userTeamIdsSet = new Set(userTeamIds);
98+
const availableTeams = allTeams.filter((t) => !userTeamIdsSet.has(t.id));
9799

98100
// Get teams with open spots
99101
const teamsWithOpenSpots = availableTeams.filter(

0 commit comments

Comments
 (0)