From 7e2941a8a6d34c66878a9981f98e181cd88a21c8 Mon Sep 17 00:00:00 2001 From: Tony Valle Date: Fri, 10 Nov 2023 13:56:45 +0100 Subject: [PATCH] perf: remove unnecessary filter iterations --- .../Stages/Stages.component.js | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stages.component.js b/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stages.component.js index a56b467acf..7290bb6ac9 100644 --- a/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stages.component.js +++ b/src/core_modules/capture-core/components/WidgetStagesAndEvents/Stages/Stages.component.js @@ -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 (<>