diff --git a/.jules/bolt.md b/.jules/bolt.md index 91541609..d4bdfa93 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. + +## 2026-06-27 - O(N*M) Array Filtering and Static Mappings +**Learning:** Using `Array.prototype.includes()` inside `Array.prototype.filter()` introduces O(N*M) complexity which is detrimental for large datasets (e.g. comparing `owners` against `voters`). Additionally, static configurations like `metaNamesSdk.config.byoc` should be mapped at the module level rather than on every request. +**Action:** Precompute an O(1) `Set` before filtering to optimize array comparisons to O(N). Move static module configurations to module-level sets or constants. diff --git a/src/routes/api/proposals/voters/add/+server.ts b/src/routes/api/proposals/voters/add/+server.ts index 62f2caf8..584e9ef8 100644 --- a/src/routes/api/proposals/voters/add/+server.ts +++ b/src/routes/api/proposals/voters/add/+server.ts @@ -22,8 +22,10 @@ export async function GET() { .get('voters') ?.setValue() .values.map((voter) => voter.addressValue().value.toString('hex')) ?? []; + const votersSet = new Set(voters); - const newVoters = owners.filter((owner) => !voters.includes(owner)).slice(0, 50); + // Optimizing O(N*M) inclusion check to O(N) Set lookup + 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..0da06ab3 100644 --- a/src/routes/api/proposals/voters/remove/+server.ts +++ b/src/routes/api/proposals/voters/remove/+server.ts @@ -19,13 +19,15 @@ export async function GET() { return json({ error: 'Voting has ended' }, { status: 400 }); const owners = await metaNamesSdk.domainRepository.getOwners(); + const ownersSet = new Set(owners); const voters = fields .get('voters') ?.setValue() .values.map((voter) => voter.addressValue().value.toString('hex')) ?? []; - const votersToRemove = voters.filter((voter) => !owners.includes(voter)).slice(0, 50); + // Optimizing O(N*M) inclusion check to O(N) Set lookup + 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({ diff --git a/src/routes/api/register/[name]/fees/[coin]/+server.ts b/src/routes/api/register/[name]/fees/[coin]/+server.ts index af646a01..9e565e8d 100644 --- a/src/routes/api/register/[name]/fees/[coin]/+server.ts +++ b/src/routes/api/register/[name]/fees/[coin]/+server.ts @@ -1,12 +1,13 @@ import { apiError, handleError, metaNamesSdk } from '$lib/server'; import type { BYOCSymbol } from '@metanames/sdk'; import { json } from '@sveltejs/kit'; -import type { DomainFeesResponse } from 'src/lib/types'; + +// Precomputing the valid coins at module level to avoid recalculation per request +const validCoinsSet = new Set(metaNamesSdk.config.byoc.map((byoc) => byoc.symbol.toString())); export async function GET({ params: { name, coin } }) { return handleError(async () => { - const validCoins = metaNamesSdk.config.byoc.map((byoc) => byoc.symbol.toString()); - if (!validCoins.includes(coin)) return apiError('Invalid coin'); + if (!validCoinsSet.has(coin)) return apiError('Invalid coin'); const normalizedDomain = metaNamesSdk.domainRepository.domainValidator.normalize(name); const domainFees = await metaNamesSdk.domainRepository.calculateMintFees(