Skip to content

Commit

Permalink
fix counting performance (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodiray authored Dec 21, 2022
1 parent e6473c5 commit 48e77f6
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
8 changes: 4 additions & 4 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ const alphabetical = (array, getter, dir = "asc") => {
return array.slice().sort(dir === "desc" ? dsc : asc);
};
const counting = (list2, identity) => {
if (!list2)
return {};
return list2.reduce((acc, item) => {
const id = identity(item);
return {
...acc,
[id]: (acc[id] ?? 0) + 1
};
acc[id] = (acc[id] ?? 0) + 1;
return acc;
}, {});
};
const replace = (list2, newItem, match) => {
Expand Down
8 changes: 4 additions & 4 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ var radash = (function (exports) {
return array.slice().sort(dir === "desc" ? dsc : asc);
};
const counting = (list2, identity) => {
if (!list2)
return {};
return list2.reduce((acc, item) => {
const id = identity(item);
return {
...acc,
[id]: (acc[id] ?? 0) + 1
};
acc[id] = (acc[id] ?? 0) + 1;
return acc;
}, {});
};
const replace = (list2, newItem, match) => {
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "10.3.0",
"version": "10.3.1",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down
7 changes: 3 additions & 4 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,11 @@ export const counting = <T, TId extends string | number | symbol>(
list: readonly T[],
identity: (item: T) => TId
): Record<TId, number> => {
if (!list) return {} as Record<TId, number>
return list.reduce((acc, item) => {
const id = identity(item)
return {
...acc,
[id]: (acc[id] ?? 0) + 1
}
acc[id] = (acc[id] ?? 0) + 1
return acc
}, {} as Record<TId, number>)
}

Expand Down
4 changes: 4 additions & 0 deletions src/tests/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,10 @@ describe('array module', () => {
Y: 2
})
})
test('does not error on bad input', () => {
_.counting(null as unknown as number[], x => x)
_.counting(undefined as unknown as number[], x => x)
})
})

describe('shift function', () => {
Expand Down

0 comments on commit 48e77f6

Please sign in to comment.