From 86c3b1de58c8a446a53ccf72da20ff3a1d5b553f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 28 Jun 2026 19:48:00 +0000 Subject: [PATCH] perf: precompute valid coins Set for API Moves the mapping of valid coins outside of the GET request handler and converts the array into a Set. This changes an O(N) lookup into an O(1) lookup on every single incoming API request. Co-authored-by: yeboster <23556525+yeboster@users.noreply.github.com> --- .jules/bolt.md | 5 +++++ src/routes/api/register/[name]/fees/[coin]/+server.ts | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) 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(