Skip to content

fix: skip sparse array holes in optimized filter - #1320

Open
xianjianlf2 wants to merge 2 commits into
immerjs:mainfrom
xianjianlf2:fix/filter-sparse-arrays-20261012
Open

xianjianlf2 wants to merge 2 commits into
immerjs:mainfrom
xianjianlf2:fix/filter-sparse-arrays-20261012

Conversation

@xianjianlf2

@xianjianlf2 xianjianlf2 commented Oct 12, 2026 •

Copy link
Copy Markdown

With enableArrayMethods(), filtering a sparse array calls the predicate for empty slots. For example, produce(new Array(3), draft => draft.filter(() => true)) returns three undefined values instead of []; a predicate that reads an object property can also throw on a hole.

Check whether each index exists before invoking the predicate, matching native filter. Explicit undefined elements are still visited, and selected objects remain drafts whose mutations update the original positions.

Synced with current main and resolved the filter-loop conflict by retaining both the upstream captured array length and the sparse-slot existence check.

Validation on Node 24.23.0 / macOS ARM64:

  • Source suite: 4,297 passed, 8 skipped.
  • Production-build suite: 3,746 passed, 8 skipped.
  • Build, focused sparse/length regressions, Prettier, and git diff --check pass.
  • The repository pre-commit formatter also normalized whitespace in frozen-array-copy.js and mapset.ts while committing the upstream merge; these contain no behavior changes.

Prepared with AI assistance, including code and regression tests.

@mweststrate mweststrate added the can-merge-if-someone-actually-cares Valid fix, but unclear cost/benefit and may hinder future work. Can merge if someone champions it. label Oct 20, 2026
@mweststrate

Copy link
Copy Markdown
Collaborator

Marked as potentially mergeable, per reasoning here: #1335 (comment). Will close if not championed.

This branch has not been deployed

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

Labels

can-merge-if-someone-actually-cares Valid fix, but unclear cost/benefit and may hinder future work. Can merge if someone champions it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants