diff --git a/.jules/bolt.md b/.jules/bolt.md index 91541609..d24a1453 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-25 - Static configuration caching + +**Learning:** In request handlers like `GET` in SvelteKit `+server.ts` files, deriving values from static configurations (like `metaNamesSdk.config.byoc.map`) on every request causes unnecessary overhead. +**Action:** Move static mapping logic out of the request handler and compute it once at module initialization, preferably storing it in a `Set` for O(1) lookup. diff --git a/src/routes/api/register/[name]/fees/[coin]/+server.ts b/src/routes/api/register/[name]/fees/[coin]/+server.ts index af646a01..454e716d 100644 --- a/src/routes/api/register/[name]/fees/[coin]/+server.ts +++ b/src/routes/api/register/[name]/fees/[coin]/+server.ts @@ -3,10 +3,12 @@ import type { BYOCSymbol } from '@metanames/sdk'; import { json } from '@sveltejs/kit'; import type { DomainFeesResponse } from 'src/lib/types'; +// Precompute valid coins as a Set for O(1) lookups and avoid redundant map on every request +const validCoins = 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 (!validCoins.has(coin)) return apiError('Invalid coin'); const normalizedDomain = metaNamesSdk.domainRepository.domainValidator.normalize(name); const domainFees = await metaNamesSdk.domainRepository.calculateMintFees(