Skip to content

Commit 9d1c1eb

Browse files
docwhatKyleAMathews
authored andcommitted
internally use Map() in getState()'s pages (#4681)
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()`.
1 parent efdef90 commit 9d1c1eb

File tree

1 file changed

+15
-16
lines changed
  • packages/gatsby/src/redux/reducers

1 file changed

+15
-16
lines changed

packages/gatsby/src/redux/reducers/pages.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
const _ = require(`lodash`)
21
const normalize = require(`normalize-path`)
32

3+
const stateToMap = state => {
4+
let stateMap = new Map()
5+
state.forEach(payload => stateMap.set(payload.path, payload))
6+
return stateMap
7+
}
8+
49
module.exports = (state = [], action) => {
510
switch (action.type) {
611
case `DELETE_CACHE`:
@@ -19,22 +24,16 @@ module.exports = (state = [], action) => {
1924
// Link page to its plugin.
2025
action.payload.pluginCreator___NODE = action.plugin.id
2126
action.payload.pluginCreatorId = action.plugin.id
22-
const index = _.findIndex(state, p => p.path === action.payload.path)
23-
// If the path already exists, overwrite it.
24-
// Otherwise, add it to the end.
25-
if (index !== -1) {
26-
return [
27-
...state
28-
.slice(0, index)
29-
.concat(action.payload)
30-
.concat(state.slice(index + 1)),
31-
]
32-
} else {
33-
return [...state.concat(action.payload)]
34-
}
27+
28+
let stateMap = stateToMap(state)
29+
stateMap.set(action.payload.path, action.payload)
30+
return Array.from(stateMap.values())
31+
}
32+
case `DELETE_PAGE`: {
33+
let stateMap = stateToMap(state)
34+
stateMap.delete(action.payload.path)
35+
return Array.from(stateMap.values())
3536
}
36-
case `DELETE_PAGE`:
37-
return state.filter(p => p.path !== action.payload.path)
3837
default:
3938
return state
4039
}

0 commit comments

Comments
 (0)