Skip to content

perf: improve ArrayUnique lookup performance - #2691

Open
itamar-gerasy wants to merge 1 commit into
typestack:developfrom
itamar-gerasy:perf/array-unique-set
Open

perf: improve ArrayUnique lookup performance#2691
itamar-gerasy wants to merge 1 commit into
typestack:developfrom
itamar-gerasy:perf/array-unique-set

Conversation

@itamar-gerasy

Copy link
Copy Markdown

Replaces the filter and indexOf implementation in arrayUnique with Set-based duplicate detection.

For arrays containing distinct values, the current implementation has O(n²) worst-case time complexity. The new implementation uses O(n) expected time while preserving identifier-based comparison and reference-based object comparison.

This also fixes the handling of NaN by using JavaScript's SameValueZero equality semantics:

  • a single NaN, or one NaN among otherwise distinct values, is accepted;
  • multiple NaN values are rejected as duplicates;
  • sparse array holes are treated as undefined, following normal Set iteration semantics.

Tests were added for object reference comparison, NaN, sparse arrays, and identifier evaluation.

Checklist

  • the pull request title describes what this PR does (not a vague title like Update index.md)
  • the pull request targets the default branch of the repository (develop)
  • the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • tests are added for the changes I made (if any source code was modified)
  • documentation added or updated (not applicable; the public API and documented behavior are unchanged)
  • I have run the project locally and verified that there are no errors

Validation completed successfully:

  • 677 functional tests
  • ES2015 build
  • ESM5 build
  • CommonJS build
  • UMD build
  • Type declarations build

Fixes

Fixes #2690

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

fix: arrayUnique is O(n²) and reports any array containing NaN as non-unique

1 participant