From fc3b53050f2d3f785b1c0c8b635347f9d755f1ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20H=C3=B6ltje?= Date: Fri, 23 Mar 2018 23:00:21 -0400 Subject: [PATCH] internally use Map() in getState()'s pages 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()`. --- packages/gatsby/src/redux/reducers/pages.js | 31 ++++++++++----------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/gatsby/src/redux/reducers/pages.js b/packages/gatsby/src/redux/reducers/pages.js index 0aafba3cff0f9..445056b7b610f 100644 --- a/packages/gatsby/src/redux/reducers/pages.js +++ b/packages/gatsby/src/redux/reducers/pages.js @@ -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`: @@ -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 }