Improved intersection#13
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.