-
Notifications
You must be signed in to change notification settings - Fork 63
Closed
Description
Description
Several hot paths use array = array.concat(...) inside loops, which copies the entire array on every iteration, resulting in O(n²) time and memory usage.
Affected locations
-
Track.buildData()(src/chunks/track.ts, lines 104, 109, 113, 121)this.data = this.data.concat(built.data);
-
Writer.buildFile()(src/writer.ts, line 45)build = build.concat(d.type, d.size, d.data);
-
Writer.base64()(src/writer.ts, lines 56-62) — string concatenation in a loopbinary += String.fromCharCode(bytes[i]);
Impact
For a track with N events, each concat copies all previously accumulated data. This turns linear work into quadratic. Unlikely to matter for small files, but noticeable for larger multi-track compositions.
Suggested Fix
Replace concat with push(...spread) or collect chunks and flatten once:
// Instead of:
this.data = this.data.concat(built.data);
// Use:
this.data.push(...built.data);For the string concatenation in base64(), collect into an array and join:
const chars = new Array(len);
for (let i = 0; i < len; i++) {
chars[i] = String.fromCharCode(bytes[i]);
}
const binary = chars.join('');Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels