Skip to content

perf(tooling): preserve sparse arrays and avoid redundant copy in assert.deepStrictEqual shim - #9479

Merged
danieljbruce merged 3 commits into
bun-runtime/1-test-runner-handwritten-libraries-4-1from
bun-runtime/1-test-runner-handwritten-libraries-4-1-1
Sep 30, 2026
Merged

danieljbruce merged 3 commits into
bun-runtime/1-test-runner-handwritten-libraries-4-1from
bun-runtime/1-test-runner-handwritten-libraries-4-1-1

Conversation

@danieljbruce

Copy link
Copy Markdown
Contributor

Summary

Stacked on top of #9460 (bun-runtime/1-test-runner-handwritten-libraries-4-1).

In bin/proxyquire-bun-shim.cjs, assert.deepStrictEqual clones actual and expected arrays when retrying comparisons for Array subclasses with constructor === Array in Bun <= 1.4.

Previously, Array.from(actual) and Array.from(expected) were used before iterating over Reflect.ownKeys and copying every own property descriptor via Object.defineProperty. This had two drawbacks:

  1. Sparse array semantics: Array.from() converts holes in sparse arrays into explicit undefined elements (creating own properties for indices that were originally empty slots), altering sparse array semantics because the subsequent Reflect.ownKeys loop only defines properties that exist on the source array and does not delete the filled holes.
  2. Redundant work: Array.from() iterates and populates all array indices upfront, only for the subsequent loop to immediately redefine every own property via Object.defineProperty.

Initializing copyA and copyB with new Array(actual.length) and new Array(expected.length) allocates an empty array of the matching length, preserving sparse array holes and avoiding redundant element copying before Object.defineProperty transfers all own property descriptors.

@danieljbruce
danieljbruce requested a review from a team as a code owner September 30, 2026 15:47
@github-actions
github-actions Bot requested a review from shivanee-p September 30, 2026 15:48
@danieljbruce

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates bin/proxyquire-bun-shim.cjs to replace Array.from() with new Array(length) when creating copies of the actual and expected arrays. This changes how the array copies are initialized before their properties are defined. There are no review comments to evaluate, and I have no feedback to provide.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces the use of 'Array.from()' with 'new Array(length)' when copying arrays in 'bin/proxyquire-bun-shim.cjs'. The feedback recommends coercing the array lengths to unsigned 32-bit integers using '>>> 0' to avoid potential 'RangeError' exceptions or unexpected behavior if the length property is not a primitive number.

Comment thread bin/proxyquire-bun-shim.cjs Outdated
) {
try {
const copyA = Array.from(actual);
const copyA = new Array(actual.length);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using new Array(actual.length) can lead to unexpected behavior or a RangeError if actual.length is not a primitive number (for example, if it is a string like "5", undefined, or a negative number). In JavaScript, new Array("5") creates an array containing the single element "5" (length 1) instead of an empty array of length 5, and new Array(undefined) creates [undefined] (length 1). To ensure robustness and guarantee that the argument is always treated as a numeric length, coerce the length to an unsigned 32-bit integer using the unsigned right shift operator (>>> 0). This is the standard ECMAScript behavior for array-like length coercion.

              const copyA = new Array(actual.length >>> 0);

Comment thread bin/proxyquire-bun-shim.cjs Outdated
}
}
const copyB = Array.from(expected);
const copyB = new Array(expected.length);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similarly, coerce expected.length to an unsigned 32-bit integer using >>> 0 to prevent unexpected behavior or RangeError if expected.length is not a primitive number.

              const copyB = new Array(expected.length >>> 0);

@danieljbruce
danieljbruce requested a review from a team as a code owner September 30, 2026 17:39
@danieljbruce
danieljbruce merged commit b2fa115 into bun-runtime/1-test-runner-handwritten-libraries-4-1 Sep 30, 2026
11 checks passed
@danieljbruce
danieljbruce deleted the bun-runtime/1-test-runner-handwritten-libraries-4-1-1 branch September 30, 2026 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant