diff --git a/.jules/bolt.md b/.jules/bolt.md index d54cf10f..2c0d1c4f 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -61,3 +61,4 @@ ## 2026-07-13 - Array.from mapping optimization **Learning:** Using `Array.from({ length: N }).map(...)` creates an intermediate array of `undefined` values which requires memory allocation and garbage collection, adding O(N) unnecessary overhead in frequently re-rendered UI components. **Action:** Use `Array.from({ length: N }, (_, index) => ...)` to map elements directly during array creation, avoiding intermediate allocations. +## 2026-08-02 - O(1) Memory Array Validation\n**Learning:** Using `Array.every()` on large byte arrays in `scoreStorage.ts` allocates unnecessary intermediate callbacks (O(N) overhead) causing garbage collection spikes.\n**Action:** Replaced `.every()` with a standard `for` loop that avoids callback overhead and exits early upon encountering a non-number.\n diff --git a/apps/desktop/src/features/score/scoreStorage.ts b/apps/desktop/src/features/score/scoreStorage.ts index 492f1259..88a1706d 100644 --- a/apps/desktop/src/features/score/scoreStorage.ts +++ b/apps/desktop/src/features/score/scoreStorage.ts @@ -91,8 +91,19 @@ export async function readScorePdf(projectId: string, scoreId: string): Promise< if (response instanceof ArrayBuffer) { return new Uint8Array(response); } - if (Array.isArray(response) && response.every((byte) => typeof byte === "number")) { - return Uint8Array.from(response as number[]); + if (Array.isArray(response)) { + // Performance: Avoid .every() on large byte arrays as it creates O(N) intermediate callback allocations. + // Use a standard for loop with an early return to achieve O(1) memory and significantly faster execution. + let isNumberArray = true; + for (let i = 0; i < response.length; i++) { + if (typeof response[i] !== "number") { + isNumberArray = false; + break; + } + } + if (isNumberArray) { + return Uint8Array.from(response as number[]); + } } throw new Error(INVALID_RESPONSE_MESSAGE);