Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duplicate map.get() causes duplicate observers? #250

Closed
nyura123 opened this issue May 15, 2016 · 3 comments
Closed

duplicate map.get() causes duplicate observers? #250

nyura123 opened this issue May 15, 2016 · 3 comments

Comments

@nyura123
Copy link

here's a small example:

const m = map({a:0})
const disposer = autorun(() => {
    console.log("RUNNING a="+m.get('a'))
    m.get('b')
    m.get('a')
})
//Display observers of m.a
console.log(JSON.stringify(m._data.a.observers.map(o=>o.id+' '+o.name)))

output:

RUNNING a=0
["5 Autorun","5 Autorun"]

Should we avoid calling .get() more than once within the same derivation? That might be difficult if it calls other functions which might get the same data.
I had a React component that observed & sorted 1000 items, and when it was unmounting, the root map had around 200K observers (so unmounting took a while).

@mweststrate
Copy link
Member

in general there is not much need to optimize those calls away. in the map example both entries are needed because a and b can be changed independently.

the sorting might be a lot more efficient if you slice the list first. probably that should be a standard optimization.

mobx does by default a lot of feed deduplication in the observers list. see observable.ts reportObserved and quickDiff in utility. it is basically a trade off between cpu and memory. from measurements it turned out that in general more aggressive deduplication made mobx slower.

@nyura123
Copy link
Author

Thanks! I'll try slicing the array - though in my case it's just an array of keys and I look up the fields to sort by in an observable map. I'll build an array with all the fields pre-looked up before calling sort on it.

Btw I tried to build mobx with a change to do a lookup of observable.id before adding it to observing and while it greatly speeds up my specific case, it slowed down the mobx tests by almost a factor of 2 (!) So yes, probably not a good tradeoff.

@mweststrate
Copy link
Member

Haha yeah I tried that as well a while back. I'm thinking of doing something more smart in the future, change the observers storage strategy from array to object if the list becomes too long or something similar :) It is a fully internal concept, so a lot of tweaks can be done in this area

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants