-
Notifications
You must be signed in to change notification settings - Fork 107
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
Improved intersection #13
base: master
Are you sure you want to change the base?
Conversation
@psy-man thanks for the contribution! Please add a test that would not have passed before, but does pass now. |
Thanks for updating the test. The bug you found is a real bug, but the solution could be improved. If we're taking the intersection of 2 arrays of lengths A more efficient solution might avoid |
Hi @bcherny I've created a few possible solutions and tested with 2 arrays: 4646 and 1103 lengths. intersectionNew - fix for current solution For me, the solution with Sets was the most faster. I'll add a commit after your decision. |
Is this a case of the average case actually nullifying the worse case big O? I did read the link you provided but if it truly was the case that From ECMA also:
Here's a benchmark from three years ago: https://github.com/anvaka/set-vs-object It seems that most browsers have optimised them beyond the performance of old school key in Object checks. I ran across across a few different browsers, mostly seems like with the length optimisation (ie choose the smaller array to iterate through), Set performs best, with the IntersectionNewSimple second best (but not by much). |
You don't need second table/set
|
Hi @bcherny.
There is a little issue in intersection solution: if the right array has any duplicates, they will appear in the result.
intersection([1, 5, 4, 2], [8, 91, 4, 1, 3, 8, 91]); // [ 4, 1, 8, 91 ]
I rewrote this function to es6 using Set.