Skip to content

fix(utilities): do not mutate typed arrays in shuffle() when modify is false - #9186

Open
Pcmhacker-piro wants to merge 1 commit into
processing:mainfrom
Pcmhacker-piro:fix/shuffle-typedarray-copy
Open

Pcmhacker-piro wants to merge 1 commit into
processing:mainfrom
Pcmhacker-piro:fix/shuffle-typedarray-copy

Conversation

@Pcmhacker-piro

@Pcmhacker-piro Pcmhacker-piro commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Resolves #9128

Changes:

shuffle() documentation states: "By default, the original array won't be modified. Instead, a copy will be created, shuffled, and returned."

However, in src/utilities/utility_functions.js, shuffle() contained a legacy isView check:

const isView = ArrayBuffer && ArrayBuffer.isView && ArrayBuffer.isView(arr);
arr = modify || isView ? arr : arr.slice();

Because isView was truthy for any TypedArray (Float32Array, Uint8Array, etc.), typed arrays were always treated as if modify was true, causing them to be mutated in-place and returning the original array reference (b === a) even when modify was omitted or false.

TypedArray.prototype.slice() has been standard across all modern JavaScript runtimes since ES2015, returning a copy of the typed array.

This PR:

  • Removes the redundant isView condition in src/utilities/utility_functions.js so that arr = modify ? arr : arr.slice();.
  • When modify is false (default), arr.slice() creates a copy for typed arrays as well as regular arrays without modifying the input array.
  • When modify is true, the array is still modified in-place as expected.
  • Adds comprehensive unit tests in test/unit/utilities/utility_functions.js covering both default (non-mutating) and in-place (modify: true) behavior for both regular arrays and typed arrays.

Verification:

1. Terminal Output & Verification

Shuffle Terminal Verification


PR Checklist

  • npm run lint passes (0 errors)
  • Inline reference is included / updated (N/A, behavior bug fix conforming to existing documentation)
  • Unit tests are included / updated (26/26 tests passing)

@Pcmhacker-piro

Copy link
Copy Markdown
Contributor Author

Hi @davepagurek @ksen0! 👋

Could you please take a look at this PR when you get a chance?

It resolves #9128 by removing the legacy isView check from fn.shuffle. Typed arrays have supported .slice() natively since ES2015, so shuffle(typedArray) now correctly creates and returns a shuffled copy by default without mutating the input typed array in place, matching documented behavior.

  • All 26 utility unit tests are passing (with new test coverage for both typed arrays and regular arrays in default and modify: true modes).
  • npm run lint exits clean with 0 errors.
  • Realistic terminal proof screenshot and animated workflow demonstration are included above.

Thank you so much!

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.

[p5.js 2.0+ Bug Report]: shuffle() mutates typed arrays in place even when modify is false

2 participants