Skip to content

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

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

xianjianlf2 wants to merge 1 commit into
immerjs:mainfrom
xianjianlf2:fix/filter-sparse-arrays-20261012

Conversation

@xianjianlf2

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.

The tests run with the plugin and auto-freezing independently enabled and disabled. Four cases fail before the fix. On Node 24.18.0, yarn test passes: 3,844 source tests and 3,293 production-build tests, with eight existing skips in each suite; the build and Flow checks pass. Prettier and git diff --check also pass.

Prepared with AI assistance, including code and regression tests.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant