5 ms·
Arrays end up being a bigger pain to deal with when updating. If the User marks TODO item #17 as complete (or deletes it), you have to either dig through the s
by gyrgtyn 9y ago
Arrays end up being a bigger pain to deal with when updating.
If the User marks TODO item #17 as complete (or deletes it), you have to either dig through the state array looking for the TODO with the matching ID, which is O(n), right? I'm sure there are times an array is more conventient. These trade offs are in the redux docs.
- acemarke 9y agoAnd this is one of many reasons why Redux encourages you to normalize your state - you can retrieve any item with a couple simple direct lookups (such as `state.entities[type].byId[id]`), rather than mapping over an array comparing item IDs. Granted, O(n) array loops aren't likely to be a real perf issue until you hit fairly large values of N. As you said, I covered this in the "Structuring Reducers - Normalizing State Shape" and "Immutable Update Patterns" sections of the docs: http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html http://redux.js.org/docs/recipes/reducers/NormalizingStateSh... and http://redux.js.org/docs/recipes/reducers/ImmutableUpdatePatterns.html http://redux.js.org/docs/recipes/reducers/ImmutableUpdatePat... .