Skip to content

Commit

Permalink
internally use Map() in getState()'s pages
Browse files Browse the repository at this point in the history
In an attempt to debug issue #4680 and generally improve
the performance of Gatsby as a whole, we swapped out the Array that
`getState()` uses internally with a `Map()`.
  • Loading branch information
docwhat committed Mar 24, 2018
1 parent 2c63272 commit fc3b530
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions packages/gatsby/src/redux/reducers/pages.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const _ = require(`lodash`)
const normalize = require(`normalize-path`)

const stateToMap = state => {
let stateMap = new Map()
state.forEach(payload => stateMap.set(payload.path, payload))
return stateMap
}

module.exports = (state = [], action) => {
switch (action.type) {
case `DELETE_CACHE`:
Expand All @@ -19,22 +24,16 @@ module.exports = (state = [], action) => {
// Link page to its plugin.
action.payload.pluginCreator___NODE = action.plugin.id
action.payload.pluginCreatorId = action.plugin.id
const index = _.findIndex(state, p => p.path === action.payload.path)
// If the path already exists, overwrite it.
// Otherwise, add it to the end.
if (index !== -1) {
return [
...state
.slice(0, index)
.concat(action.payload)
.concat(state.slice(index + 1)),
]
} else {
return [...state.concat(action.payload)]
}

let stateMap = stateToMap(state)
stateMap.set(action.payload.path, action.payload)
return Array.from(stateMap.values())
}
case `DELETE_PAGE`: {
let stateMap = stateToMap(state)
stateMap.delete(action.payload.path)
return Array.from(stateMap.values())
}
case `DELETE_PAGE`:
return state.filter(p => p.path !== action.payload.path)
default:
return state
}
Expand Down

0 comments on commit fc3b530

Please sign in to comment.