Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/compat/array/intersection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { intersection as intersectionToolkit } from '../../array/intersection.ts';
import { uniq } from '../../array/uniq.ts';
import { toArray } from '../_internal/toArray.ts';
import { isArrayLikeObject } from '../predicate/isArrayLikeObject.ts';

/**
Expand Down Expand Up @@ -28,7 +29,7 @@ export function intersection<T>(...arrays: Array<ArrayLike<T> | null | undefined
return [];
}

let result: T[] = uniq(Array.from(arrays[0]));
let result: T[] = uniq(toArray(arrays[0]));

for (let i = 1; i < arrays.length; i++) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
for (let i = 1; i < arrays.length; i++) {
for (let i = 1; i < arrays.length && result.length > 0; i++) {

What do you think about adding a result.length > 0 check?
Once there's nothing left to compare, we can skip the rest of the loop.
This is more efficient for that edge case, while in the general case it adds only a single extra check, which doesn't seem like it would have any meaningful performance impact.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We would like to keep the implementation simple and straightforward if possible, so I would like to stick with the current approach.

const array = arrays[i];
Expand All @@ -37,7 +38,7 @@ export function intersection<T>(...arrays: Array<ArrayLike<T> | null | undefined
return [];
}

result = intersectionToolkit(result, Array.from(array));
result = intersectionToolkit(result, toArray(array));
}

return result;
Expand Down