Skip to main content
Lightning Jar - Web Studio Lightning Jar Wordmark

Findings No. 73

Node throws on a large spread call that Bun allows, so `bun test` can miss the limit

bytes.push(...big) passed under bun test but threw RangeError in Node 22 from about 130,000 arguments. Size limit tests past both engines' limits.

Barmark Testing verified Node 22 (V8); Bun 1.3.14 (JavaScriptCore)

The MIDI file writer in @barmark/midi appended each event's bytes with bytes.push(...eventBytes(event)). Under bun test, a 200,000-byte text meta event wrote without error. The built package running under Node 22 threw RangeError: Maximum call stack size exceeded from about 130,000 bytes. A code review found it. No test did.

Spread call arguments go on the stack, and each engine sets its own limit. In barmark's measurements the limit was about 130,000 arguments in Node (V8) and about 1,000,000 in Bun (JavaScriptCore). A suite that runs only under Bun therefore accepts inputs that Node cannot handle. The midi-file package, which barmark's tests use to read files back, has the same limit when it reads text through String.fromCharCode.apply, so it cannot read back a megabyte meta event either.

The fix in src/smf/write.ts never spreads a payload into a call. It pushes bytes one at a time and assembles chunks with Uint8Array.set. The regression test writes a 1,000,000-byte meta event, large enough to fail under Bun as well as Node if anything spreads it, and checks the output bytes directly instead of parsing them with midi-file.

General rule: when code ships to more than one engine, size each limit test past the largest engine's limit, so the test fails on whichever runtime the suite happens to use.

Receipt: barmark commit b81780c (packages/midi/src/smf/write.ts, packages/midi/tests/smf-write.test.ts; FINDINGS.md, "Node refuses a spread Bun allows, so bun test couldn't see the limit").

Ask Eljay

Ask about the studio's work, research, packages, or writing. A few starters:

Answers come from this site's own content and link their sources. For anything that matters, email hello@lightningjar.com.