From 7d34fe6339912bf8cd3d45480fab72dd2cadd391 Mon Sep 17 00:00:00 2001 From: Peter van der Zee Date: Wed, 14 Oct 2020 15:26:41 +0200 Subject: [PATCH] perf(gatsby-source-contentful): do not linear search on a potentially large set --- .../gatsby-source-contentful/src/gatsby-node.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/gatsby-source-contentful/src/gatsby-node.js b/packages/gatsby-source-contentful/src/gatsby-node.js index 6de12f80d616b..cefec99c9e675 100644 --- a/packages/gatsby-source-contentful/src/gatsby-node.js +++ b/packages/gatsby-source-contentful/src/gatsby-node.js @@ -196,25 +196,23 @@ exports.sourceNodes = async ( // Create a map of up to date entries and assets function mergeSyncData(previous, current, deleted) { const entryMap = new Map() - previous.forEach( - e => !deleted.includes(e.sys.id) && entryMap.set(e.sys.id, e) - ) - current.forEach( - e => !deleted.includes(e.sys.id) && entryMap.set(e.sys.id, e) - ) + previous.forEach(e => !deleted.has(e.sys.id) && entryMap.set(e.sys.id, e)) + current.forEach(e => !deleted.has(e.sys.id) && entryMap.set(e.sys.id, e)) return [...entryMap.values()] } + const deletedSet = new Set(currentSyncData.deletedEntries.map(e => e.sys.id)) + const mergedSyncData = { entries: mergeSyncData( previousSyncData.entries, currentSyncData.entries, - currentSyncData.deletedEntries.map(e => e.sys.id) + deletedSet ), assets: mergeSyncData( previousSyncData.assets, currentSyncData.assets, - currentSyncData.deletedAssets.map(e => e.sys.id) + deletedSet ), }