-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(utils): intersection 기능 개선 및 밴치마크 추가 (#311)
* fix(utils): intersection 기능 개선 및 밴치마크 추가 * docs: 문서 수정 * chore: vitest coverage benchmark 파일 제외
- Loading branch information
Showing
5 changed files
with
54 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/utils/src/array/intersection/intersection.bench.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { bench, describe } from 'vitest'; | ||
import { intersectionBy as intersectionByLodash } from 'lodash-es'; | ||
import { intersection } from '.'; | ||
|
||
const createTestArr = () => { | ||
return Array.from({ length: 50 }, () => ({ | ||
id: Math.floor(Math.random() * 10), | ||
})); | ||
}; | ||
|
||
describe('intersection', () => { | ||
const arr1 = createTestArr(); | ||
const arr2 = createTestArr(); | ||
|
||
bench('modern-kit/intersection', () => { | ||
intersection(arr1, arr2, (item) => item.id); | ||
}); | ||
|
||
bench('lodash/intersectionBy', () => { | ||
intersectionByLodash(arr1, arr2, 'id'); | ||
}); | ||
}); |
18 changes: 10 additions & 8 deletions
18
packages/utils/src/array/intersectionWithDuplicates/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import { identity } from '../../common'; | ||
|
||
export const intersectionWithDuplicates = <T, U = T>( | ||
export const intersectionWithDuplicates = <T, U>( | ||
firstArr: T[] | readonly T[], | ||
secondArr: T[] | readonly T[], | ||
iteratee: (item: T) => T | U = identity, | ||
iteratee?: (item: T) => U | ||
) => { | ||
const secondArrSetAppliedIteratee = new Set(secondArr.map(iteratee)); | ||
|
||
return firstArr.filter((item) => | ||
secondArrSetAppliedIteratee.has(iteratee(item)), | ||
const secondArrToSet = new Set<T | U>( | ||
iteratee ? secondArr.map(iteratee) : secondArr | ||
); | ||
|
||
const filterFn = iteratee | ||
? (element: T) => secondArrToSet.has(iteratee(element)) | ||
: (element: T) => secondArrToSet.has(element); | ||
|
||
return firstArr.filter(filterFn); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters