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

feat: bring up Array.shuffle #102

Closed
wants to merge 4 commits into from
Closed

Conversation

SukkaW
Copy link

@SukkaW SukkaW commented Aug 20, 2020

The PR closes #100, bringing up a side-effect-free, and O(n) array shuffle function.

The unit test is not finished since tad is not powerful enough to write a unit test for shuffle (Help is wanted).

P.S. I want to adopt the unit test from https://github.com/philihp/fast-shuffle/blob/master/src/__tests__/index.test.js

Copy link
Owner

@medikoo medikoo left a comment

Choose a reason for hiding this comment

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

Great thanks @SukkaW for this!

There's just one thing, which unfortunately I didn't communicate well.

What's in master branch is considered deprecated, and rather in maintanance mode. Currently development centers around ext branch (from which ext package is published.

Would mind rebasing this PR against ext branch, and following conventions over there?

If you have any questions towards that, please ask (I know that docs may lack in placases).

Again, very sorry for confusion. I'll make ext branch default today, and add note in main README.md so it's no longer that confusing

@SukkaW SukkaW changed the base branch from master to ext August 21, 2020 09:37
@SukkaW
Copy link
Author

SukkaW commented Aug 21, 2020

@medikoo

Well, should I bring up Array.shuffle under _es5_ext after switch base to ext branch?

@medikoo
Copy link
Owner

medikoo commented Aug 21, 2020

Well, should I bring up Array.shuffle under _es5_ext after switch base to ext branch?

@SukkaW No, it should be implemented in array/shuffle.js file (_es5_ext contains items that are to be adapted in ext but where not yet)

@SukkaW
Copy link
Author

SukkaW commented Aug 21, 2020

@medikoo The PR is ready for review now.

Unit test cases and the docs are completed.

@SukkaW
Copy link
Author

SukkaW commented Aug 21, 2020

It appears that the newest mocha (which utilises destructuring assignment syntax) is not compatible with Node.js 4 causes CI to fail.


while (sourceIndex) {
// eslint-disable-next-line no-bitwise
var randomIndex = (sourceIndex * random()) | 0;
Copy link
Author

Choose a reason for hiding this comment

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

With bitwise no more Math.ceil is required, which makes the shuffle even faster.

Copy link
Owner

Choose a reason for hiding this comment

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

How much do we gain, will it be observable faster? Do we have some numbers?

Copy link
Author

@SukkaW SukkaW Aug 21, 2020

Choose a reason for hiding this comment

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

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks, still this benchmark doesn't seem to test purely Math.round vs bitwise stripping.

I've prepared one here: https://jsbench.me/mske89xfr3/1 and it shows negligible difference between both (note that both operations on its own are extremely fast)

It seems not harmful to stick to Math.round here, and it'll definitely be win for readability

Copy link
Author

Choose a reason for hiding this comment

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

I have updated the original benchmark with Math.floor: https://repl.it/repls/CornyBrightConditions

It seems that Math.floor is still slower (At least it is slower in Node.js)

Copy link
Owner

@medikoo medikoo Aug 24, 2020

Choose a reason for hiding this comment

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

This benchmark still involves a lot more code than it should. Technically it's purely about testing Math.floor(value) against value | 0.

Node.js uses same engine as Chrome, and difference that https://jsbench.me/mske89xfr3/1 shows over there is neglible.

Also this package is environment agnostic, meant to be used in any engine (e.g. in Firefox there's no speed difference between both)

Copy link
Owner

Choose a reason for hiding this comment

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

I'd use Math.floor now, and eventually revisit if we would have a real world report that it noticeable slows down performance somewhere.

Copy link
Owner

@medikoo medikoo left a comment

Choose a reason for hiding this comment

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

Thank you @SukkaW it looks great! See my remarks

README.md Outdated Show resolved Hide resolved
array/shuffle.js Outdated Show resolved Hide resolved
array/shuffle.js Outdated Show resolved Hide resolved
array/shuffle.js Show resolved Hide resolved

while (sourceIndex) {
// eslint-disable-next-line no-bitwise
var randomIndex = (sourceIndex * random()) | 0;
Copy link
Owner

Choose a reason for hiding this comment

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

How much do we gain, will it be observable faster? Do we have some numbers?

test/array/shuffle.js Outdated Show resolved Hide resolved
Copy link
Owner

@medikoo medikoo left a comment

Choose a reason for hiding this comment

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

Thank you @SukkaW it looks great! I have just one (more a readability) concern with using bitwise notation. see my comment.


while (sourceIndex) {
// eslint-disable-next-line no-bitwise
var randomIndex = (sourceIndex * random()) | 0;
Copy link
Owner

Choose a reason for hiding this comment

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

Thanks, still this benchmark doesn't seem to test purely Math.round vs bitwise stripping.

I've prepared one here: https://jsbench.me/mske89xfr3/1 and it shows negligible difference between both (note that both operations on its own are extremely fast)

It seems not harmful to stick to Math.round here, and it'll definitely be win for readability

array/shuffle.js Show resolved Hide resolved
@SukkaW SukkaW closed this Jul 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Array.shuffle
2 participants