map.set is very slow compared to map constructor
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.
Quick question, does using .merge improve the performance? Can you share some code about how you update the map?
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 ?
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
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);
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.
@umphy @asterius1 indeed, sounds like your code is not wrapped in either an action or transaction
@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 :)
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 .
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 @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
}