mobx icon indicating copy to clipboard operation
mobx copied to clipboard

map.set is very slow compared to map constructor

Open umphy opened this issue 10 years ago • 10 comments

I am using React with MobX, where I render a list of items backed by a store with a map.

I am fetching a data set of around 2000 objects, where each object has around 30 properties. I then loop through each object and call a myMap.set(key, value). This is very slow, even though it is in a transaction.

Then instead of setting straight into my map, I created a plain JS object, set all the objects in there, then called myMap = map(newDataObj). This is very fast.

umphy avatar Aug 10 '16 06:08 umphy

Quick question, does using .merge improve the performance? Can you share some code about how you update the map?

mweststrate avatar Aug 10 '16 08:08 mweststrate

Interested in this one. I'm using asMap helper from mobx, and iterate collections from backend like issue creator. Is there are any performance measurements comparing to arrays ?

KatSick avatar Aug 10 '16 09:08 KatSick

Suddenly I understand about what @mweststrate talking. So to fix problems with huge collections and asMap, you probably need to:

const normalized = {};
my2000objectsArray.forEach(item => normilized[item.id] = item)
this.normalizedOvservable = asMap(normalized)

I'm coding from my phone, so sorry to be that short in my thoughts

KatSick avatar Aug 10 '16 20:08 KatSick

OK so my original code, which was very slow, is:

updateList.forEach(function(item) {
    self.dataMap.set(item.id, item);
});

If I change it to constructing a new map, or even using merge, it is fast:

let newMap = {};
updateList.forEach(function(item) {
    newMap[item.id] = item;
});
// both options below are fast
self.dataMap = map(newMap);
self.dataMap.merge(newMap);

umphy avatar Aug 11 '16 01:08 umphy

Does this fix it?

mobx.transaction(() => {
  updateList.forEach(function(item) {
    self.dataMap.set(item.id, item);
  });
})

From what I've seen this is how merge/constructor is implemented itself.

asterius1 avatar Aug 11 '16 14:08 asterius1

@umphy @asterius1 indeed, sounds like your code is not wrapped in either an action or transaction

mweststrate avatar Aug 11 '16 18:08 mweststrate

@asterius1 yes that did fix it. What happened was my code snippet was actually called within an AJAX callback, which was itself within a function marked with @action. Obviously that @action didn't apply to the async AJAX callback. I wonder if this is a common pitfall...? So my code looks something like:

@action
function fetchData() {
  let self = this;
  $.post(url, function(updateList) {
    updateList.forEach(function(item) {
      self.dataMap.set(item.id, item);
    });
  });
}

Thanks guys :)

umphy avatar Aug 12 '16 01:08 umphy

Yep it is a common mistake. Actually described in the docs ;-). Note that strict mode prevents this mistake as well as in that case the set would have thrown an exception.

Op vr 12 aug. 2016 om 03:22 schreef umphy [email protected]:

@asterius1 https://github.com/asterius1 yes that did fix it. What happened was my code snippet was actually called within an AJAX callback, which was itself within a function marked with @action. Obviously that @action didn't apply to the async AJAX callback. I wonder if this is a common pitfall...? So my code looks something like:

@action function fetchData() { let self = this; $.post(url, function(updateList) { updateList.forEach(function(item) { self.dataMap.set(item.id, item); }); }); }

Thanks guys :)

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/mobxjs/mobx/issues/471#issuecomment-239339543, or mute the thread https://github.com/notifications/unsubscribe-auth/ABvGhKah2EswkZerwX3c4w2-7QvwQuWAks5qe8rPgaJpZM4JgzJP .

mweststrate avatar Aug 12 '16 08:08 mweststrate

Hi @mweststrate , Currently I'm facing the same issue when updating store. it is slow and makes my application freeze. Here is my snippet code: @observable locations = []; @action async getLocations() { const fetchedLocations = await callingAnAPI(); -> it will return ~10.000 data this.locations = fetchedLocations; } What did i do wrong?

adrianha avatar Feb 12 '17 13:02 adrianha

@adrianha @action has no effect on async actions, as it is internally rewritten to async promises. So in that case you need something like:

@observable locations = [];

async getLocations() {
    const fetchedLocations = await callingAnAPI(); -> it will return ~10.000 data
    runInAction("update locations", () => { this.locations = fetchedLocations; }) // or create a real @action method for this
}

mweststrate avatar Mar 07 '17 13:03 mweststrate