diff --git a/.jules/bolt.md b/.jules/bolt.md index 91541609..74cfc541 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-10-26 - O(N*M) Array Filtering Optimization + +**Learning:** Using `Array.prototype.includes()` inside an `Array.prototype.filter()` loop over large datasets creates an O(N*M) performance bottleneck. In this specific application, comparing large arrays of `owners` against `voters` synchronously blocks the main thread. +**Action:** Always replace O(N) lookup operations like `.includes()` inside loops with an O(1) `Set.prototype.has()` check by precomputing a Set before the loop, reducing overall complexity to O(N + M). diff --git a/.prettierignore b/.prettierignore index 38972655..82db87e5 100644 --- a/.prettierignore +++ b/.prettierignore @@ -11,3 +11,6 @@ node_modules pnpm-lock.yaml package-lock.json yarn.lock +.jules/ +static/ +test-results/ diff --git a/src/routes/api/proposals/voters/add/+server.ts b/src/routes/api/proposals/voters/add/+server.ts index 62f2caf8..51f57457 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); + // Optimize O(N) includes within the filter loop with an O(1) Set lookup + 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..be429ce5 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); + // Optimize O(N) includes within the filter loop with an O(1) Set lookup + 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({