0

in reading Mobx's documentation for its recommended best practices for defining data stores (see https://mobx.js.org/defining-data-stores.html), I see that the domain objects are stored in an array (in the example code "Example domain store, the TODOStore class contains a field "todos" which is an array). In that same class, functions like updateTodoFromServer and removeTodo do a linear scan matching by id by calling .find() or indexOf(). (The removeTodo function also has to call splice too!)

Normally, in a not-mobx context, based on this usage I would say that the todos field should actually be a map that allows you to look it up or delete it by "id" in constant time (or at least logarithmic time) rather than linear time.

BTW, in my own code, the number of items in the mobx store could theoretically be very large (if all possible items were pulled in)

  1. Is there a reason the mobx documentation examples use an array here?
  2. What if I used a map/ObservableMap instead? Is that better? Neutral? Not recommended?
  3. If maps are indeed better... does a normal map or an ObservableMap make more sense here?

Thanks in advance for helping me understand mobx better.

I tried reading mobx documentation and asking the question in a search engine and reading multiple search results, but I didn't find an explanation to my question yet.

muddybruin
  • 785
  • 4
  • 8

1 Answers1

1

From my experience choosing between types.map and types.array mostly depends on what operations are you going make on that data. If you're going to store objects like users and have some CRUD operations over them then I would suggest to use types.map because finding/updating an item in types.map is simpler then in types.array and it should be more performant.

I'd also check some general considerations about choosing Map over plain array

ddavydov
  • 66
  • 3