Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(shuffle): use the Fisher-Yates algorithm #76

Merged
merged 3 commits into from
Jul 5, 2024

Conversation

eumkz
Copy link
Contributor

@eumkz eumkz commented Jul 3, 2024

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Summary

This PR replaces the current implementation of the shuffle function with a more efficient version using the Fisher-Yates algorithm.

Key changes:

  1. Switched from a functional to an imperative approach for better performance.
  2. Implemented Fisher-Yates shuffle algorithm for in-place shuffling.
  3. Added option to pass custom random function, defaulting to Radashi's random function.
  4. Ensured original array is not mutated by creating a copy.

Performance implications:

  • Improved time complexity from O(n log n) to O(n).
  • Reduced memory usage by avoiding creation of intermediate arrays.
  • Potentially faster for larger arrays due to fewer object creations and method calls.

Testing:

  • Existing tests should pass without modification.
  • Added new tests for custom random function and array immutability.

Note: This change may impact the specific shuffle results but maintains the same randomness guarantees.

Related issue, if any:

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed

Does this PR introduce a breaking change?

No

@eumkz eumkz requested a review from aleclarson as a code owner July 3, 2024 23:37
Copy link
Member

@Minhir Minhir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is nice!

aleclarson
aleclarson previously approved these changes Jul 4, 2024
Copy link
Member

@aleclarson aleclarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks @eumkz. 👍

@aleclarson aleclarson merged commit 27b1710 into radashi-org:main Jul 5, 2024
3 of 4 checks passed
@aleclarson aleclarson changed the title Optimize shuffle function using Fisher-Yates algorithm perf(shuffle): use the Fisher-Yates algorithm Jul 5, 2024
Copy link

github-actions bot commented Jul 5, 2024

A new beta version 12.2.0-beta.85fb266 has been published to NPM. 🚀

To install:

pnpm add radashi@12.2.0-beta.85fb266

The radashi@beta tag also includes this PR.

See the changes

@aleclarson aleclarson added the 🔥 performance An improvement to runtime performance label Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔥 performance An improvement to runtime performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants