question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: computed (aka memoized) properties

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Reactions:75
  • Comments:48 (10 by maintainers)

github_iconTop GitHub Comments

16reactions
marcelcremercommented, Oct 11, 2019

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):

@Input({
    default: ''
})
firstName: string;

@Input({
    default: '';
})
lastName: string;

get fullName() {
    return `${this.firstName} ${this.lastName}`;
}

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

@Input()
set firstName(value: string)
{
    this._firstName = value || '';
    this.calculateFullName();
}
get firstName() {
    return this._firstName;
}
private _firstName: string;

@Input()
set lastName(value: string)
{
    this._lastName = value || '';
    this.calculateFullName();
}
get lastName() {
    return this._lastName;
}
private _lastName: string;

fullName: string = '';

calculateFullName() {
 this.fullName = `${this.firstName} ${this.lastName}`;
}

which is really ugly, error prone, bloated and no fun to use…

So +1 for computed Properties🎉

11reactions
marcelcremercommented, Dec 9, 2019

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 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found