Skip to content

Commit 48e77f6

Browse files
authored
fix counting performance (#204)
1 parent e6473c5 commit 48e77f6

File tree

6 files changed

+17
-14
lines changed

6 files changed

+17
-14
lines changed

Diff for: cdn/radash.esm.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ const alphabetical = (array, getter, dir = "asc") => {
135135
return array.slice().sort(dir === "desc" ? dsc : asc);
136136
};
137137
const counting = (list2, identity) => {
138+
if (!list2)
139+
return {};
138140
return list2.reduce((acc, item) => {
139141
const id = identity(item);
140-
return {
141-
...acc,
142-
[id]: (acc[id] ?? 0) + 1
143-
};
142+
acc[id] = (acc[id] ?? 0) + 1;
143+
return acc;
144144
}, {});
145145
};
146146
const replace = (list2, newItem, match) => {

Diff for: cdn/radash.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ var radash = (function (exports) {
138138
return array.slice().sort(dir === "desc" ? dsc : asc);
139139
};
140140
const counting = (list2, identity) => {
141+
if (!list2)
142+
return {};
141143
return list2.reduce((acc, item) => {
142144
const id = identity(item);
143-
return {
144-
...acc,
145-
[id]: (acc[id] ?? 0) + 1
146-
};
145+
acc[id] = (acc[id] ?? 0) + 1;
146+
return acc;
147147
}, {});
148148
};
149149
const replace = (list2, newItem, match) => {

Diff for: cdn/radash.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "radash",
3-
"version": "10.3.0",
3+
"version": "10.3.1",
44
"description": "Functional utility library - modern, simple, typed, powerful",
55
"main": "dist/cjs/index.cjs",
66
"module": "dist/esm/index.mjs",

Diff for: src/array.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,11 @@ export const counting = <T, TId extends string | number | symbol>(
161161
list: readonly T[],
162162
identity: (item: T) => TId
163163
): Record<TId, number> => {
164+
if (!list) return {} as Record<TId, number>
164165
return list.reduce((acc, item) => {
165166
const id = identity(item)
166-
return {
167-
...acc,
168-
[id]: (acc[id] ?? 0) + 1
169-
}
167+
acc[id] = (acc[id] ?? 0) + 1
168+
return acc
170169
}, {} as Record<TId, number>)
171170
}
172171

Diff for: src/tests/array.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,10 @@ describe('array module', () => {
693693
Y: 2
694694
})
695695
})
696+
test('does not error on bad input', () => {
697+
_.counting(null as unknown as number[], x => x)
698+
_.counting(undefined as unknown as number[], x => x)
699+
})
696700
})
697701

698702
describe('shift function', () => {

0 commit comments

Comments
 (0)