Proposal: computed (aka memoized) properties
See original GitHub issueI’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
There is no real computed properties. Getters are bad for performance. Example https://plnkr.co/edit/TQMQFb?p=preview
Expected behavior
To be able to use computed properties as in vue or mobx
What is the motivation / use case for changing the behavior?
My proposal is to make change detection smarter by adding computed properties like in mobx or vue.
import {Computed, Reactive} from '@angular/core';
...
class MyCmp {
age = 69;
@Reactive firstName = 'hello';
@Reactive lastName = 'angular';
@Computed get fullName() {
console.log('recomputed');
return `${this.firstName} ${this.lastName}`;
}
}
This will allow to have computed get property and be able to cache it, so it is computed only when needed. Currently this is not possible and getter is recomputed a lot of times, especially if using default change detection.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:75
- Comments:48 (10 by maintainers)
Top Results From Across the Web
Memoization of Swift properties - Pitches
You don't really have to call it a "computed property". A memoized property is basically both computed and stored. As you said, the...
Read more >Kirill Rakhman on Twitter: "After having worked with @vuejs, I'm ...
The proposed solutions, heavy-weight state management (think Vuex) or ... Proposal: computed (aka memoized) properties · Issue #20472 · angular/angular.
Read more >How I wrote the world's fastest React memoization library
It just passes all the props (except compute ) to the compute function, and renders result via function-as-children (aka renderProps). Thats is ...
Read more >Dynamic Programming vs Memoization
Memoization means the optimization technique where you memorize previously computed results, which will be used whenever the same result ...
Read more >adapton - Rust - Docs.rs
Memoization provides a mechanism for caching the results of subcomputations; it is a crtical feature of Adapton's approach to incremental computation. In ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was switching between a ng and a vue project back and forth the last weeks and cannot believe, how natural the computed properties in vue work and that angular hasn’t something similiar to offer.
In vue, you can easily do something like this(translated to ng terminology):
which, despite the fact that we don’t have default values in ng too, expresses exactly what we are doing.
However, in angular you have to do something like
which is really ugly, error prone, bloated and no fun to use…
So +1 for computed Properties🎉
If you have a hammer, everything looks like a nail…
While this might work, it has so much overhead that I don’t even want to try it… IMHO it’s a workaround for a missing feature in angular, which is the reason why some people think angular is overengineered.
Still props and ❤️ for posting your workaround attempt 😃