Skip to content

Commit 48295ba

Browse files
Dohun-choimanudeli
andauthored
perf(uniq): improve performance (#1358)
* perf(uniq): improve performance * fix: add test case * fix: Fix uniq function to produce consistent results with Array.from --------- Co-authored-by: Jonghyeon Ko <[email protected]>
1 parent 881f840 commit 48295ba

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/array/uniq.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,15 @@ describe('uniq', () => {
3636

3737
expect(array).toEqual([1, 2, 3, 2, 1, 3]);
3838
});
39+
40+
it('should handle arrays with undefined And Hole', () => {
41+
// eslint-disable-next-line no-sparse-arrays
42+
const arr: any[] = [1, , 2, undefined, 3, , 2];
43+
expect(uniq(arr)).toEqual([1, undefined, 2, 3]);
44+
});
45+
46+
it('should handle arrays with special values', () => {
47+
const arr = [NaN, NaN, 0, -0, Infinity, -Infinity];
48+
expect(uniq(arr)).toEqual([NaN, 0, Infinity, -Infinity]);
49+
});
3950
});

src/array/uniq.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
* // result will be [1, 2, 3, 4, 5]
1515
*/
1616
export function uniq<T>(arr: readonly T[]): T[] {
17-
return Array.from(new Set(arr));
17+
return [...new Set(arr)];
1818
}

0 commit comments

Comments
 (0)