From c51faa524a23e22cc177b67a5a75aaa352b552f0 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 5 Jul 2026 19:45:44 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[performance=20improvement]?= =?UTF-8?q?=20Replace=20O(N)=20includes=20with=20O(1)=20Set=20lookup=20in?= =?UTF-8?q?=20voter=20APIs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced `Array.prototype.includes()` inside array `.filter()` loops with `Set.prototype.has()` in the voter addition and removal APIs (`src/routes/api/proposals/voters/add/+server.ts` and `src/routes/api/proposals/voters/remove/+server.ts`). This solves an O(N*M) time complexity issue when checking large arrays. Co-authored-by: yeboster <23556525+yeboster@users.noreply.github.com> --- .jules/bolt.md | 4 ++++ src/routes/api/proposals/voters/add/+server.ts | 5 ++++- src/routes/api/proposals/voters/remove/+server.ts | 5 ++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 91541609..f30f3a3e 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -9,3 +9,7 @@ **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 for O(1) Lookups in Large Array Filtering + +**Learning:** When filtering large lists (like `owners` against `voters`), using `Array.prototype.includes()` inside `.filter()` results in an O(N*M) complexity. This causes significant performance degradation as both lists grow. +**Action:** Always precompute a `Set` before the filter loop to convert the O(N) array lookup into an O(1) set lookup, improving the overall complexity to O(N). diff --git a/src/routes/api/proposals/voters/add/+server.ts b/src/routes/api/proposals/voters/add/+server.ts index 62f2caf8..db219936 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); + // ⚡ Bolt Performance Optimization + // Use Set for O(1) lookups instead of O(N) Array.includes to avoid O(N*M) complexity + 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..a5ac0633 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); + // ⚡ Bolt Performance Optimization + // Use Set for O(1) lookups instead of O(N) Array.includes to avoid O(N*M) complexity + 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({