Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

Commit 5f9bc22

Browse files
authored
Optimize ensureArray by not copying array (#962)
This PR updates the `ensureArray` utility function so that it no longer creates a shallow copy of the provided array. I reviewed all uses of `ensureArray` and confirmed that callers were not mutating the returned array.
1 parent 4a14d0a commit 5f9bc22

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/utils/__tests__/ensureArray.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ test('should return an array if passed an array', () => {
44
expect(ensureArray(['foo', 'bar'])).toEqual(['foo', 'bar']);
55
});
66

7+
test('should return the same array reference when passed an array', () => {
8+
const input = ['foo', 'bar'];
9+
expect(ensureArray(input)).toBe(input); // referential equality
10+
});
11+
712
test('should return an empty array if no value passed', () => {
813
expect(ensureArray()).toEqual([]);
914
});

src/utils/ensureArray.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const ensureArray = <T>(value?: T | readonly T[]): T[] => {
22
if (Array.isArray(value)) {
3-
return [...value];
3+
return value;
44
}
55

66
if (typeof value === 'undefined') {

0 commit comments

Comments
 (0)