processing / processing/p5.js

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

Open
#9,128 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
24k
Forks
3.8k
Avg merge
3d 16h
Merged PRs (30d)
25

Description

Most appropriate sub-area of p5.js?

Utilities

p5.js version

2.x main (4b096e2)

Actual vs expected behavior

shuffle() documents: "By default, the original array won't be modified. Instead, a copy will be created, shuffled, and returned." But the implementation treats every typed array as if modify were true:

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

Executed repro:

const a = new Float32Array([1,2,3,4,5,6,7,8]);
const b = shuffle(a);      // no modify flag
// a is now shuffled in place, and b === a

The isView special case predates TypedArray.prototype.slice (ES2015); every typed array has had .slice() for a decade, so the copy path works fine for them today.

Steps to reproduce

Run the snippet above in any 2.x sketch. Output from an executed run against main: original 1,2,3,4,5,6,7,8 became 1,8,3,7,4,6,2,5 and the return value is the same object.

Note

I have a fix ready (drop the isView special case) with two unit tests (typed array not modified by default, still modified in place with modify: true), mutation-tested against main. shuffle is not deprecated in 2.0. Filing for approval first per the contributing guide; will open the PR once approved.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the shuffle implementation shown in the report and inspect the existing shuffle unit tests. Run the typed-array reproduction, then verify that the default call leaves the original unchanged and returns a distinct result while modify: true still mutates in place.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.