mirror of
https://github.com/toss/es-toolkit.git
synced 2025-01-06 07:57:12 +03:00
perf(intersectionBy): Optimize time complexity (#44)
This commit is contained in:
parent
d22d241ea6
commit
0a02cdcaea
@ -19,9 +19,6 @@
|
||||
* // result will be [{ id: 2 }] since only this element has a matching id in both arrays.
|
||||
*/
|
||||
export function intersectionBy<T, U>(firstArr: T[], secondArr: T[], mapper: (item: T) => U): T[] {
|
||||
const mappedSecondArr = secondArr.map(x => mapper(x));
|
||||
|
||||
return firstArr.filter(item => {
|
||||
return mappedSecondArr.includes(mapper(item));
|
||||
});
|
||||
const mappedSecondSet = new Set(secondArr.map(mapper));
|
||||
return firstArr.filter(item => mappedSecondSet.has(mapper(item)));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user