From e8bef4de50d51ef4af0529cd1e6b2e96b5932ac7 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 30 Mar 2026 19:52:06 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20optimize=20array=20sort=20i?= =?UTF-8?q?n=20DomainsTable.svelte?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces inefficient `.slice()` and `.reverse()` array instantiations inside the `Array.prototype.sort()` comparator with direct variable assignment and conditional swapping. This drastically reduces garbage collection overhead and improves performance when sorting large domain lists. Co-authored-by: yeboster <23556525+yeboster@users.noreply.github.com> --- .jules/bolt.md | 5 +++++ src/routes/profile/DomainsTable.svelte | 14 +++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 91541609..0e7f7a52 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-11-20 - Array Sort Garbage Collection Overhead + +**Learning:** Creating new arrays (e.g., `[a[sort], b[sort]]`) and using array methods like `.slice()` or `.reverse()` inside `Array.prototype.sort()` comparator functions creates severe garbage collection overhead, especially for large datasets. This is because the comparator is called $O(N \log N)$ times per sort. +**Action:** Always use direct variable assignment and conditional swapping (e.g., `const temp = a; a = b; b = temp;`) inside sort comparators to prevent excessive object allocation. diff --git a/src/routes/profile/DomainsTable.svelte b/src/routes/profile/DomainsTable.svelte index c42bda5a..1dd83570 100644 --- a/src/routes/profile/DomainsTable.svelte +++ b/src/routes/profile/DomainsTable.svelte @@ -29,9 +29,17 @@ function handleSort() { domains.sort((a, b) => { - const [aVal, bVal] = [a[sort], b[sort]][ - sortDirection === 'ascending' ? 'slice' : 'reverse' - ](); + let aVal = a[sort]; + let bVal = b[sort]; + + // ⚡ Bolt: Prevent massive garbage collection overhead by avoiding array + // instantiation and .reverse() / .slice() inside the O(N log N) sort comparator. + if (sortDirection !== 'ascending') { + const temp = aVal; + aVal = bVal; + bVal = temp; + } + if (typeof aVal === 'string' && typeof bVal === 'string') return aVal.localeCompare(bVal); return Number(aVal) - Number(bVal); });