Skip to content

Commit

Permalink
perf(gatsby-source-contentful): change O(n*m) loop to O(n+m) (#27448)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdz authored Oct 15, 2020
1 parent 9d304ed commit e6162dd
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/gatsby-source-contentful/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,21 @@ const makeMakeId = ({ currentLocale, defaultLocale, createNodeId }) => (
type
) => createNodeId(makeId({ spaceId, id, currentLocale, defaultLocale, type }))

exports.buildEntryList = ({ contentTypeItems, mergedSyncData }) =>
contentTypeItems.map(contentType =>
mergedSyncData.entries.filter(
entry => entry.sys.contentType.sys.id === contentType.sys.id
)
exports.buildEntryList = ({ contentTypeItems, mergedSyncData }) => {
// Create buckets for each type sys.id that we care about (we will always want an array for each, even if its empty)
const map = new Map(
contentTypeItems.map(contentType => [contentType.sys.id, []])
)
// Now fill the buckets. Ignore entries for which there exists no bucket. (Not sure if that ever happens)
mergedSyncData.entries.map(entry => {
let arr = map.get(entry.sys.contentType.sys.id)
if (arr) {
arr.push(entry)
}
})
// Order is relevant, must map 1:1 to contentTypeItems array
return contentTypeItems.map(contentType => map.get(contentType.sys.id))
}

exports.buildResolvableSet = ({
entryList,
Expand Down

0 comments on commit e6162dd

Please sign in to comment.