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

Don't recalculate derived properties until next access #233

Open
STRML opened this issue Mar 15, 2016 · 4 comments
Open

Don't recalculate derived properties until next access #233

STRML opened this issue Mar 15, 2016 · 4 comments

Comments

@STRML
Copy link
Contributor

STRML commented Mar 15, 2016

I plan to submit a PR for this - opening this issue to track it.

The issue is that derived properties are often expensive, and any change to any dep causes a recalculate, even if the property has not been accessed.

Considering that derived props are already getters, dep changes should instead set a dirty flag (since delete self._cache[name] would cause deopt) which marks the property for recomputation.

@STRML
Copy link
Contributor Author

STRML commented Mar 15, 2016

The only issue I can think of is change listeners - any ideas on this? Should we be checking for the presence of a change or change:${derivedKey} listener, then revert to the current behavior if present?

@latentflip
Copy link
Contributor

The only issue I can think of is change listeners - any ideas on this? Should we be checking for the presence of a change or change:${derivedKey} listener, then revert to the current behavior if present?

Are there many situations where you would have a derived property on a model, but would have no change/change:${derivedKey} listeners on that model at all?

@latentflip
Copy link
Contributor

I'm not going to suggest that this has as nice an API as a derived property does - I've not tried to abstract it in any way - but an alternative way of doing this if you don't need the change events, and just want a cached getter, is to just create a cached getter, using Object.defineProperty on the prototype, and something like lodash.memoize for the caching.

If you do need the change events, then I'm not sure that you can avoid the current behaviour.

Here's an example:

http://esnextb.in/?gist=acf4e2cae3bab5eae5f4

@STRML
Copy link
Contributor Author

STRML commented Mar 16, 2016

Yes - I don't use change listeners at all, I'm using ampersand-state with a Flux pattern so I know exactly when changes have been made.

I often use derived properties to mask and cache expensive operations that are not used often - until now I had assumed that recalculation was only done at access, not if any dep had changed.

It should be relatively simple to record when change handlers have been attached. Can you think of any issues with this approach?

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