diff --git a/.jules/bolt.md b/.jules/bolt.md index a55b7bae..5c3c4bdf 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -42,6 +42,10 @@ **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.] + ## 2026-03-12 - O(1) early exit for confidence level **Learning:** Using `.reduce()` unconditionally iterates over the entire array for operations with an absolute bound (e.g. finding if there's any 'low' confidence section). **Action:** Replace unconditional `.reduce()` with a `for...of` loop and early `break` to short-circuit upon finding the minimum possible bound, changing O(N) worst-case into an O(K) best-case execution, yielding measurable performance gains on large documents. diff --git a/packages/shared-types/src/index.ts b/packages/shared-types/src/index.ts index 0d8d6f1c..cba4606a 100644 --- a/packages/shared-types/src/index.ts +++ b/packages/shared-types/src/index.ts @@ -388,7 +388,18 @@ 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 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; + } + } + return true; } /** Documented. */ diff --git a/packages/shared-types/test/index.test.ts b/packages/shared-types/test/index.test.ts index 72dd81be..564ee182 100644 --- a/packages/shared-types/test/index.test.ts +++ b/packages/shared-types/test/index.test.ts @@ -113,6 +113,83 @@ 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("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(