Skip to content

Commit 711a6da

Browse files
authored
perf(cpex): course lookup with map instead of linear search (#3685)
1 parent b20b4ce commit 711a6da

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

website/src/views/mpe/form/MpeFormContainer.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@ const MpeFormContainer: React.FC<Props> = ({ getSubmission, updateSubmission })
2525
Promise.all([fetchMpeModuleList(), getSubmission()])
2626
.then(([fetchedMpeModuleList, fetchedSubmission]) => {
2727
setMpeModuleList(fetchedMpeModuleList);
28+
const moduleLookup = new Map(
29+
fetchedMpeModuleList.map((module) => [module.moduleCode, module]),
30+
);
2831
setSubmission({
2932
...fetchedSubmission,
3033
// Replace data fetched from MPE server with latest data from MPE module list
31-
// TODO: Optimize. This does an linear time lookup for each preference
3234
preferences: fetchedSubmission.preferences.map((preference) => {
33-
const mpeModule = fetchedMpeModuleList.find(
34-
(m) => m.moduleCode === preference.moduleCode,
35-
);
35+
const mpeModule = moduleLookup.get(preference.moduleCode);
3636
if (!mpeModule) return preference;
3737
return {
3838
...preference,
3939
moduleTitle: mpeModule.title,
40-
moduleCode: mpeModule.moduleCode,
4140
moduleCredits: parseInt(mpeModule.moduleCredit, 10),
4241
};
4342
}),

0 commit comments

Comments
 (0)