Skip to content

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

Description

@rajanpanth

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions