9
comments : {
    byId : {
        "comment1" : {
            id : "comment1",
            author : "user2",
            comment : ".....",
        },
        "comment2" : {
            id : "comment2",
            author : "user3",
            comment : ".....",
        },
        "comment3" : {
            id : "comment3",
            author : "user3",
            comment : ".....",
        },
        "comment4" : {
            id : "comment4",
            author : "user1",
            comment : ".....",
        },
        "comment5" : {
            id : "comment5",
            author : "user3",
            comment : ".....",
        },
    },
    allIds : ["comment1", "comment2", "comment3", "commment4", "comment5"]
}

In the above example, is there any reason my needs to include it api include it. I assume this way you can do a count faster, you can probably sort but generally I am not understanding if there is a performance hit.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
nolawi
  • 3,843
  • 5
  • 18
  • 43
  • 1
    Where does that requirement come from? It's certainly not a JavaScript or React thing. I can't speak for Redux. – T.J. Crowder Sep 08 '17 at 21:55
  • This isn't a requirement from Redux either. – Etheryte Sep 08 '17 at 22:21
  • 2
    I've seen it in several questions. Apparently it's how Dan Abramov does it in this tutorial: https://egghead.io/lessons/javascript-redux-normalizing-the-state-shape – jonahe Sep 08 '17 at 23:01

1 Answers1

8

This isn't anything that's required by Redux, this is a normalizr thing. To answer your question, JavaScript objects can't be replied upon to retain sort order in certain situations. Putting the ids in an array allows you to retain the sort order that was present before you normalized.

Quote from co-maintainer of Redux and author of "normalizing state shape section" of Redux docs:

As for the ID arrays, while JS engines now have a fairly standardized process for iterating across keys in an object, you shouldn't rely on that to define ordering. Storing arrays of IDs allows you to define an order for items.

Rob Wise
  • 4,560
  • 3
  • 21
  • 30
  • 2
    *"To answer your question, JavaScript objects don't guarantee the order in which properties appear."* Yes, they do, as of ES2015, see [here](http://www.ecma-international.org/ecma-262/8.0/index.html#sec-ordinaryownpropertykeys) and [here](http://www.ecma-international.org/ecma-262/8.0/index.html#sec-enumerate-object-properties) and Oriol's explanation [here](https://stackoverflow.com/a/30919039/157247). New operations added in ES2015, and `JSON.stringify`, follow the order. Legacy operations (`Object.keys`, `for-in`) are not required to. But **JSON** does not have property order. :-) – T.J. Crowder Sep 09 '17 at 08:26
  • 2
    normalizr was created in 2014, React apps can be used in as old browsers as IE 9, normalizr does not return JSON so JSON's lack of property order is not relevant here, the specs don't require respecting sort order when enumerating over your data (which is extremely common in React apps), and the author of the library explicitly states unreliable sort ordering as his reasoning [here](https://stackoverflow.com/questions/42238802/redux-many-to-many-relationship/42257112?noredirect=1#comment71675068_42257112) I'll change to "can't be relied upon to retain" – Rob Wise Sep 10 '17 at 05:09
  • correction: Mark wrote the [Normalizing State Shape](http://redux.js.org/docs/recipes/reducers/NormalizingStateShape.html) doc for Redux and is a co-maintainer of Redux with its creator, Dan Abramov, who also created Normalizr. – Rob Wise Sep 10 '17 at 05:17