From 5b887c5e819e8f2288944dffa66644f3cd07c9ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anne=20L=27H=C3=B4te?= Date: Wed, 6 Dec 2023 18:34:43 +0100 Subject: [PATCH] feat(api): Improve performance --- server/src/routes/works.routes.js | 7 ++++--- server/src/utils/utils.js | 10 ++++++---- server/src/utils/works.js | 3 ++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/server/src/routes/works.routes.js b/server/src/routes/works.routes.js index 48e53115..3d8b29e0 100644 --- a/server/src/routes/works.routes.js +++ b/server/src/routes/works.routes.js @@ -36,8 +36,9 @@ router.route('/works') console.time(`4. Sort works ${options.affiliations}`); const publications = []; const datasets = []; - // TODO IMPROVE PERF by replacing the forEach by a for loop - deduplicatedWorks.forEach((deduplicatedWork) => { + const deduplicatedWorksLength = deduplicatedWorks.length; + for (let i = 0; i < deduplicatedWorksLength; i += 1) { + const deduplicatedWork = deduplicatedWorks[i]; if ( (deduplicatedWork.datasource.includes('fosm') && deduplicatedWork.type !== 'dataset') || (deduplicatedWork.datasource.includes('openalex') && deduplicatedWork.type !== 'dataset') @@ -51,7 +52,7 @@ router.route('/works') } else { console.error(`Work not sorted : ${JSON.stringify(deduplicatedWork)}`); } - }); + } console.timeEnd(`4. Sort works ${options.affiliations}`); // Compute distinct types & years for facet console.time(`5. Facet ${options.affiliations}`); diff --git a/server/src/utils/utils.js b/server/src/utils/utils.js index ade336b1..d076e579 100644 --- a/server/src/utils/utils.js +++ b/server/src/utils/utils.js @@ -113,10 +113,12 @@ const defaultDiacriticsRemovalMap = [ ]; const diacriticsMap = {}; -for (let i = 0; i < defaultDiacriticsRemovalMap.length; i += 1) { - const { letters } = defaultDiacriticsRemovalMap[i]; - for (let j = 0; j < letters.length; j += 1) { - diacriticsMap[letters[j]] = defaultDiacriticsRemovalMap[i].base; +const defaultDiacriticsRemovalMapLength = defaultDiacriticsRemovalMap.length; +for (let i = 0; i < defaultDiacriticsRemovalMapLength; i += 1) { + const { base, letters } = defaultDiacriticsRemovalMap[i]; + const lettersLength = letters.length; + for (let j = 0; j < lettersLength; j += 1) { + diacriticsMap[letters[j]] = base; } } diff --git a/server/src/utils/works.js b/server/src/utils/works.js index 3aa37e0c..d5a81b39 100644 --- a/server/src/utils/works.js +++ b/server/src/utils/works.js @@ -216,7 +216,8 @@ const groupByAffiliations = ({ options, works }) => { // Compute distinct affiliations of works const allAffiliationsTmp = works.reduce((deduplicatedAffiliations, work) => { const { affiliations = [] } = work; - for (let i = 0; i < affiliations.length; i += 1) { + const affiliationsLength = affiliations.length; + for (let i = 0; i < affiliationsLength; i += 1) { const affiliation = affiliations[i]; const normalizedAffiliation = removeDiacritics(affiliation); if (normalizedAffiliations.some((aff) => normalizedAffiliation.includes(aff))) {