Skip to content

Commit e6162dd

Browse files
authoredOct 15, 2020
perf(gatsby-source-contentful): change O(n*m) loop to O(n+m) (#27448)
1 parent 9d304ed commit e6162dd

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed
 

‎packages/gatsby-source-contentful/src/normalize.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,21 @@ const makeMakeId = ({ currentLocale, defaultLocale, createNodeId }) => (
114114
type
115115
) => createNodeId(makeId({ spaceId, id, currentLocale, defaultLocale, type }))
116116

117-
exports.buildEntryList = ({ contentTypeItems, mergedSyncData }) =>
118-
contentTypeItems.map(contentType =>
119-
mergedSyncData.entries.filter(
120-
entry => entry.sys.contentType.sys.id === contentType.sys.id
121-
)
117+
exports.buildEntryList = ({ contentTypeItems, mergedSyncData }) => {
118+
// Create buckets for each type sys.id that we care about (we will always want an array for each, even if its empty)
119+
const map = new Map(
120+
contentTypeItems.map(contentType => [contentType.sys.id, []])
122121
)
122+
// Now fill the buckets. Ignore entries for which there exists no bucket. (Not sure if that ever happens)
123+
mergedSyncData.entries.map(entry => {
124+
let arr = map.get(entry.sys.contentType.sys.id)
125+
if (arr) {
126+
arr.push(entry)
127+
}
128+
})
129+
// Order is relevant, must map 1:1 to contentTypeItems array
130+
return contentTypeItems.map(contentType => map.get(contentType.sys.id))
131+
}
123132

124133
exports.buildResolvableSet = ({
125134
entryList,

0 commit comments

Comments
 (0)
Please sign in to comment.