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

Does MobX Optimise Array.map? #429

Closed
ronag opened this issue Jul 20, 2016 · 1 comment
Closed

Does MobX Optimise Array.map? #429

ronag opened this issue Jul 20, 2016 · 1 comment

Comments

@ronag
Copy link

ronag commented Jul 20, 2016

e.g. if I do this:

@computed get items () {
  return ids.map(id => new Item(id))
}

Will MobX reuse items or recreate everything every time? i.e.

ids = [0]
ids = [0, 1] // new Item('0') could be re-used
ids = [1] // new Item('1') could be reused

This issue and debouncing is what's keeping me from leaving RxJS.

@mweststrate
Copy link
Member

No, map by default creates a fresh, dumb, plain javascript array. However, using create transformer you can make this mapping memoizable:

const idToItemTransformer =  createTransformer(id => new Item(id))

@computed get items() {
   return ids.map(idToItemTransformer)
}

Note that the transformation automatically garbage collects, so make sure that the computed value is in use by some reaction to benefit from the optimization. For further details: https://mobxjs.github.io/mobx/refguide/create-transformer.html.

In the future "liveMap" and similar operations will be added that returns an observable array. This has the advantage that only changes need to be mapped, instead of the complete array. See #166

Debouncing is available for reaction and autorunAsync. See the docs.

For extensive async, time driven events RxJS is still a good choice. See also: https://github.com/mobxjs/mobx/wiki/Mobx-vs-Reactive-Stream-Libraries-(RxJS,-Bacon,-etc)

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

No branches or pull requests

2 participants