From b0716838baade2138494371b17ceba6eb0793baf Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 6 Jul 2026 19:49:46 +0000 Subject: [PATCH] Refactor array filtering in API to use Set for O(1) lookups Co-authored-by: yeboster <23556525+yeboster@users.noreply.github.com> --- .jules/bolt.md | 3 +++ src/routes/api/proposals/voters/add/+server.ts | 4 +++- src/routes/api/proposals/voters/remove/+server.ts | 4 +++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 91541609..4b07d671 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -9,3 +9,6 @@ **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. +## 2026-07-06 - Array Filtering Complexity +**Learning:** Using `Array.prototype.includes()` inside `Array.prototype.filter()` creates O(N*M) complexity, which becomes a bottleneck on large datasets (like voters vs owners). +**Action:** Precompute a `Set` before the loop and use `Set.prototype.has()` to reduce complexity to O(N+M) for array filtering operations. diff --git a/src/routes/api/proposals/voters/add/+server.ts b/src/routes/api/proposals/voters/add/+server.ts index 62f2caf8..15bcbb91 100644 --- a/src/routes/api/proposals/voters/add/+server.ts +++ b/src/routes/api/proposals/voters/add/+server.ts @@ -23,7 +23,9 @@ export async function GET() { ?.setValue() .values.map((voter) => voter.addressValue().value.toString('hex')) ?? []; - const newVoters = owners.filter((owner) => !voters.includes(owner)).slice(0, 50); + // Optimization: Precompute Set for O(1) lookups instead of O(N) includes() + 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..2fe16ae6 100644 --- a/src/routes/api/proposals/voters/remove/+server.ts +++ b/src/routes/api/proposals/voters/remove/+server.ts @@ -25,7 +25,9 @@ export async function GET() { ?.setValue() .values.map((voter) => voter.addressValue().value.toString('hex')) ?? []; - const votersToRemove = voters.filter((voter) => !owners.includes(voter)).slice(0, 50); + // Optimization: Precompute Set for O(1) lookups instead of O(N) includes() + 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({