diff --git a/.jules/bolt.md b/.jules/bolt.md index 91541609..094e503a 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 - Array Sort Callback Optimization + +**Learning:** Creating temporary arrays or using object destructuring inside an `Array.prototype.sort()` callback (e.g., `[a, b][condition ? 'slice' : 'reverse']()`) introduces heavy Garbage Collection overhead and slows down operations. +**Action:** Always refactor sorting logic to use straightforward scalar variable assignments and mathematical negations (e.g., multiplying by -1) for descending order. diff --git a/src/routes/profile/DomainsTable.svelte b/src/routes/profile/DomainsTable.svelte index c42bda5a..07e7964d 100644 --- a/src/routes/profile/DomainsTable.svelte +++ b/src/routes/profile/DomainsTable.svelte @@ -29,11 +29,15 @@ function handleSort() { domains.sort((a, b) => { - const [aVal, bVal] = [a[sort], b[sort]][ - sortDirection === 'ascending' ? 'slice' : 'reverse' - ](); - if (typeof aVal === 'string' && typeof bVal === 'string') return aVal.localeCompare(bVal); - return Number(aVal) - Number(bVal); + // Optimization: Avoid temporary array creation for performance. + const aVal = a[sort]; + const bVal = b[sort]; + const direction = sortDirection === 'ascending' ? 1 : -1; + + if (typeof aVal === 'string' && typeof bVal === 'string') { + return aVal.localeCompare(bVal) * direction; + } + return (Number(aVal) - Number(bVal)) * direction; }); domains = domains; }