Skip to content

Proposal: computed (aka memoized) properties #20472

@anjmao

Description

@anjmao

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions