From af65ada1a14341bff948dce53b5eb95c77e98034 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 26 Jun 2026 20:10:22 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20optimize=20large=20array=20?= =?UTF-8?q?intersection=20with=20Set=20lookups?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: yeboster <23556525+yeboster@users.noreply.github.com> --- .jules/bolt.md | 5 +++++ src/routes/api/proposals/voters/add/+server.ts | 4 +++- src/routes/api/proposals/voters/remove/+server.ts | 4 +++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 91541609..cb13a851 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-05-24 - Array Intersection Complexity + +**Learning:** When filtering or comparing elements between two potentially large arrays (e.g., `owners.filter(owner => !voters.includes(owner))`), `Array.prototype.includes()` nested inside an array iteration method creates O(N\*M) time complexity. +**Action:** Always convert the second array to a `Set` before the loop (e.g., `const votersSet = new Set(voters)`) to enable O(1) lookups via `Set.prototype.has()`, reducing the overall 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..bc169bb0 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); + // ⚡ Bolt: Optimize large array intersection by precomputing a 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..a0fe5020 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); + // ⚡ Bolt: Optimize large array intersection by precomputing a 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({