Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(gatsby-source-contentful): change O(n*m) loop to O(n+m) #27448

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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