Skip to content

Commit

Permalink
perf(cpex): course lookup with map instead of linear search (#3685)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlhw authored Mar 27, 2024
1 parent b20b4ce commit 711a6da
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions website/src/views/mpe/form/MpeFormContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ const MpeFormContainer: React.FC<Props> = ({ getSubmission, updateSubmission })
Promise.all([fetchMpeModuleList(), getSubmission()])
.then(([fetchedMpeModuleList, fetchedSubmission]) => {
setMpeModuleList(fetchedMpeModuleList);
const moduleLookup = new Map(
fetchedMpeModuleList.map((module) => [module.moduleCode, module]),
);
setSubmission({
...fetchedSubmission,
// Replace data fetched from MPE server with latest data from MPE module list
// TODO: Optimize. This does an linear time lookup for each preference
preferences: fetchedSubmission.preferences.map((preference) => {
const mpeModule = fetchedMpeModuleList.find(
(m) => m.moduleCode === preference.moduleCode,
);
const mpeModule = moduleLookup.get(preference.moduleCode);
if (!mpeModule) return preference;
return {
...preference,
moduleTitle: mpeModule.title,
moduleCode: mpeModule.moduleCode,
moduleCredits: parseInt(mpeModule.moduleCredit, 10),
};
}),
Expand Down

0 comments on commit 711a6da

Please sign in to comment.