Skip to content

Commit

Permalink
perf: remove unnecessary filter iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
superskip committed Nov 10, 2023
1 parent 83424eb commit 7e2941a
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ import { withLoadingIndicator } from '../../../HOC';

const styles = {};
export const StagesPlain = ({ stages, events, classes, ...passOnProps }: PlainProps) => {
const eventsByStage = useMemo(() => stages.reduce(
(acc, stage) => {
acc[stage.id] = events.filter(event => event.programStage === stage.id);
return acc;
}, {}), [stages, events],
const eventsByStage = useMemo(
() => stages.reduce(
(acc, stage) => {
acc[stage.id] = acc[stage.id] || [];
return acc;
},
events.reduce(
(acc, event) => {
const stageId = event.programStage;
if (acc[stageId]) {
acc[stageId].push(event);
} else {
acc[stageId] = [event];
}
return acc;
},
{},
),
),
[stages, events],
);

return (<>
Expand Down

0 comments on commit 7e2941a

Please sign in to comment.