From 70f62717157fb24d5cbe0174d1023a9932c25ec0 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 24 Jun 2026 19:43:07 +0000 Subject: [PATCH] perf(api): replace Array.includes with Set.has in voter filtering This commit optimizes array filtering in `voters/add/+server.ts` and `voters/remove/+server.ts` by replacing the O(N) `Array.prototype.includes` lookups with O(1) `Set.has` lookups. This improves performance from O(N*M) to O(N+M) when operating on large lists of domain owners and proposal voters, mitigating main thread blocking during these API requests. The corresponding learning pattern has been documented in the `.jules/bolt.md` journal. Co-authored-by: yeboster <23556525+yeboster@users.noreply.github.com> --- .jules/bolt.md | 5 +++++ src/routes/api/proposals/voters/add/+server.ts | 5 ++++- src/routes/api/proposals/voters/remove/+server.ts | 5 ++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 91541609..74b936c6 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -9,3 +9,8 @@ **Learning:** Using `on:keyup` for search input debouncing triggers unnecessary API calls on navigation keys (arrows, home, end) and misses changes from paste/cut. Svelte's reactive statements `$: debounce(value)` provide a robust, declarative way to trigger debouncing only when the value actually changes. **Action:** Replace `on:keyup` handlers with reactive statements for input debouncing to improve performance and correctness. + +## 2024-11-20 - Set.prototype.has over Array.prototype.includes + +**Learning:** Using `Array.prototype.includes` inside `Array.prototype.filter` or loop iterations over large datasets creates an O(N*M) time complexity, potentially blocking the main thread significantly. +**Action:** Always precompute a `Set` before loop operations and replace O(N) `Array.includes` with O(1) `Set.has` to keep time complexity to O(N+M). diff --git a/src/routes/api/proposals/voters/add/+server.ts b/src/routes/api/proposals/voters/add/+server.ts index 62f2caf8..87a2fd68 100644 --- a/src/routes/api/proposals/voters/add/+server.ts +++ b/src/routes/api/proposals/voters/add/+server.ts @@ -23,7 +23,10 @@ export async function GET() { ?.setValue() .values.map((voter) => voter.addressValue().value.toString('hex')) ?? []; - const newVoters = owners.filter((owner) => !voters.includes(owner)).slice(0, 50); + // Performance optimization: Precompute a Set for O(1) lookups instead of O(N) Array.prototype.includes + // This reduces time complexity from O(N*M) to O(N+M) + const votersSet = new Set(voters); + const newVoters = owners.filter((owner) => !votersSet.has(owner)).slice(0, 50); if (newVoters.length === 0) return json({ newVoters }, { status: 200 }); const votingContract = await metaNamesSdk.contractRepository.getContract({ diff --git a/src/routes/api/proposals/voters/remove/+server.ts b/src/routes/api/proposals/voters/remove/+server.ts index d024e90f..04737e78 100644 --- a/src/routes/api/proposals/voters/remove/+server.ts +++ b/src/routes/api/proposals/voters/remove/+server.ts @@ -25,7 +25,10 @@ export async function GET() { ?.setValue() .values.map((voter) => voter.addressValue().value.toString('hex')) ?? []; - const votersToRemove = voters.filter((voter) => !owners.includes(voter)).slice(0, 50); + // Performance optimization: Precompute a Set for O(1) lookups instead of O(N) Array.prototype.includes + // This reduces time complexity from O(N*M) to O(N+M) + const ownersSet = new Set(owners); + const votersToRemove = voters.filter((voter) => !ownersSet.has(voter)).slice(0, 50); if (votersToRemove.length === 0) return json({ newVoters: votersToRemove }, { status: 200 }); const votingContract = await metaNamesSdk.contractRepository.getContract({