From 4259801f5cb9f50ffd32f8b9b6b6f8b1fa7a2686 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 27 Jun 2026 20:24:43 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[performance=20improvement]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Optimized array filtering by replacing O(N) `Array.includes` inside `Array.filter` with an O(1) `Set.has` lookup, reducing filtering complexity from O(N*M) to O(N). Also moved static array mapping in the fees endpoint to a precomputed module-level Set to prevent repeated per-request processing overhead. Co-authored-by: yeboster <23556525+yeboster@users.noreply.github.com> --- .jules/bolt.md | 4 ++++ src/routes/api/proposals/voters/add/+server.ts | 4 +++- src/routes/api/proposals/voters/remove/+server.ts | 4 +++- src/routes/api/register/[name]/fees/[coin]/+server.ts | 7 ++++--- 4 files changed, 14 insertions(+), 5 deletions(-) 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(