From 77994e50158e677ace79190089d5ceea5b2300d7 Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Thu, 9 Jul 2026 04:06:42 +0000 Subject: [PATCH 1/3] perf: optimize `isDenseArray` function to eliminate O(N) allocation Replaced `Array.from().every()` logic with an imperative `for` loop that implements early returns (`i in value`). This completely eliminates the instantiation of intermediate arrays of size N, preventing unnecessary memory allocation and garbage collection overhead during repeated evaluations on large arrays. The function behavior remains functionally identical. --- .jules/bolt.md | 4 ++++ packages/shared-types/src/index.ts | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 38d4b732..d83134cc 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -41,3 +41,7 @@ ## 2025-02-15 - Replace Array.from(map.values()).map with a for...of loop **Learning:** Using `Array.from(map.values()).map(...)` creates an unnecessary intermediate array which wastes memory allocation and garbage collection time, particularly for frequently re-rendered components handling large collections. **Action:** Use a `for...of` loop over `map.values()` to iterate and push mapped elements directly into the final array for O(1) memory and avoiding intermediate array allocations. + +## 2026-07-09 - [Array density check optimization] +**Learning:** [Using Array.from().every() to check for array density creates O(N) intermediate array allocations which add unnecessary garbage collection overhead on the critical path.] +**Action:** [Use a standard for-loop with an early return (i in value) for an O(1) memory and faster check.] diff --git a/packages/shared-types/src/index.ts b/packages/shared-types/src/index.ts index ff04618b..061ec40c 100644 --- a/packages/shared-types/src/index.ts +++ b/packages/shared-types/src/index.ts @@ -379,7 +379,14 @@ function isRecord(value: unknown): value is Record { /** Documented. */ function isDenseArray(value: unknown): value is unknown[] { - return Array.isArray(value) && Array.from({ length: value.length }, (_, index) => index in value).every(Boolean); + if (!Array.isArray(value)) return false; + // Performance: Avoid O(N) allocation of intermediate array from Array.from() + for (let i = 0; i < value.length; i++) { + if (!(i in value)) { + return false; + } + } + return true; } /** Documented. */ From 66fae2d8f3efc076cf1bdaa93b34289448e04eb4 Mon Sep 17 00:00:00 2001 From: Seongho Bae Date: Mon, 13 Jul 2026 14:12:23 +0900 Subject: [PATCH 2/3] fix(shared-types): snapshot dense array length --- packages/shared-types/src/index.ts | 3 ++- packages/shared-types/test/index.test.ts | 25 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/shared-types/src/index.ts b/packages/shared-types/src/index.ts index b7d396ae..8f19fb27 100644 --- a/packages/shared-types/src/index.ts +++ b/packages/shared-types/src/index.ts @@ -390,7 +390,8 @@ function isRecord(value: unknown): value is Record { function isDenseArray(value: unknown): value is unknown[] { if (!Array.isArray(value)) return false; // Performance: Avoid O(N) allocation of intermediate array from Array.from() - for (let i = 0; i < value.length; i++) { + const arrayLength = value.length; + for (let i = 0; i < arrayLength; i++) { if (!(i in value)) { return false; } diff --git a/packages/shared-types/test/index.test.ts b/packages/shared-types/test/index.test.ts index 72dd81be..15047c03 100644 --- a/packages/shared-types/test/index.test.ts +++ b/packages/shared-types/test/index.test.ts @@ -113,6 +113,31 @@ describe("shared type helpers", () => { expect(() => parseProjectSummary({ ...validSummary, extraField: true })).toThrow("extraField"); }); + it("snapshots array length while validating dense array boundaries", () => { + let lengthReads = 0; + let entriesStarted = false; + const supportedAudioFormats = new Proxy(["wav"], { + get(target, property, receiver) { + if (property === "entries") { + entriesStarted = true; + } + if (property === "length") { + lengthReads += 1; + return !entriesStarted && lengthReads > 1 ? 2 : Reflect.get(target, property, receiver); + } + return Reflect.get(target, property, receiver); + } + }) as ProjectSummary["supportedAudioFormats"]; + const summary: ProjectSummary = { + id: "project-1", + title: "Demo Song", + status: "running", + supportedAudioFormats + }; + + expect(validateProjectSummary(summary)).toBeNull(); + }); + it("property-checks supported local audio sources", () => { fc.assert( fc.property( From becf82ed6cdb9c7b730b573e2630a480c6a19c7f Mon Sep 17 00:00:00 2001 From: Seongho Bae Date: Mon, 13 Jul 2026 14:40:46 +0900 Subject: [PATCH 3/3] fix(shared-types): normalize dense array length --- packages/shared-types/src/index.ts | 7 +++- packages/shared-types/test/index.test.ts | 52 ++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/packages/shared-types/src/index.ts b/packages/shared-types/src/index.ts index 8f19fb27..cba4606a 100644 --- a/packages/shared-types/src/index.ts +++ b/packages/shared-types/src/index.ts @@ -389,8 +389,11 @@ function isRecord(value: unknown): value is Record { /** Documented. */ function isDenseArray(value: unknown): value is unknown[] { if (!Array.isArray(value)) return false; - // Performance: Avoid O(N) allocation of intermediate array from Array.from() - const arrayLength = value.length; + // Performance: avoid Array.from() allocation while keeping a one-time length boundary. + const arrayLength = Number(value.length); + if (!Number.isSafeInteger(arrayLength) || arrayLength < 0 || arrayLength > 0xffffffff) { + return false; + } for (let i = 0; i < arrayLength; i++) { if (!(i in value)) { return false; diff --git a/packages/shared-types/test/index.test.ts b/packages/shared-types/test/index.test.ts index 15047c03..564ee182 100644 --- a/packages/shared-types/test/index.test.ts +++ b/packages/shared-types/test/index.test.ts @@ -138,6 +138,58 @@ describe("shared type helpers", () => { expect(validateProjectSummary(summary)).toBeNull(); }); + it("rejects array proxies with non-primitive changing lengths", () => { + let lengthCoercions = 0; + const changingLength = { + valueOf() { + lengthCoercions += 1; + return lengthCoercions === 1 ? 2 : 0; + } + }; + const supportedAudioFormats = new Proxy(["wav"], { + get(target, property, receiver) { + if (property === "length") { + return changingLength; + } + return Reflect.get(target, property, receiver); + } + }) as ProjectSummary["supportedAudioFormats"]; + const summary: ProjectSummary = { + id: "project-1", + title: "Demo Song", + status: "running", + supportedAudioFormats + }; + + expect(validateProjectSummary(summary)).toBe( + "Invalid project summary contract: invalid field 'supportedAudioFormats'" + ); + expect(lengthCoercions).toBe(1); + }); + + it("rejects array proxies with non-finite or out-of-range lengths", () => { + for (const length of [Number.POSITIVE_INFINITY, 0xffffffff + 1, 1.5]) { + const supportedAudioFormats = new Proxy(["wav"], { + get(target, property, receiver) { + if (property === "length") { + return length; + } + return Reflect.get(target, property, receiver); + } + }) as ProjectSummary["supportedAudioFormats"]; + const summary: ProjectSummary = { + id: "project-1", + title: "Demo Song", + status: "running", + supportedAudioFormats + }; + + expect(validateProjectSummary(summary)).toBe( + "Invalid project summary contract: invalid field 'supportedAudioFormats'" + ); + } + }); + it("property-checks supported local audio sources", () => { fc.assert( fc.property(