Skip to content

Using a getter function brings the CPU usage to over 100% - Production builds / Chrome ONLY #24614

Closed

Description

@un33k

I'm submitting a...


[ x ] Possible bug report  
[ x ] Performance issue

Current behavior

Looping over a list of objects

import { Component } from '@angular/core';

@Component({
  selector: 'home',
  templateUrl: './home.component.html',
})
export class HomeComponent {
  get someList() {
    return [
      {
        title: 'foo'
      },
      {
        title: 'bar'
      }
    ];
  }
}
<div *ngFor="let item of someList">{{item.title}}</div>

screen shot 2018-06-21 at 3 01 01 pm

Expected behavior

If there is a problem with the above usage, neither the linter nor the compiler caught it.
If the above usage is alright, then it should not consume lots of CPU cycles freezing the browser.

Minimal reproduction of the problem with instructions

  • Copy the above code into a simple project
  • ng build --prod
  • cd into the dist directory
  • serve the directory (ex: python -m SimpleHTTPServer)
  • open localhost:port via browser (Chrome)
  • check the cpu usage

Workaround

import { Component } from '@angular/core';

@Component({
  selector: 'home',
  templateUrl: './home.component.html',
})
export class HomeComponent {
  someList = [
    {
      title: 'foo'
    },
    {
      title: 'bar'
    }
  ];
}
<div *ngFor="let item of someList">{{item.title}}</div>

It would have been great if the problem was caught during the build.
Such errors is particularly hard to debug since the browser freezes and becomes unresponsive.

Environment


Angular CLI: 6.0.8
Node: 9.11.2
OS: darwin x64
Angular: 6.0.5
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router
... service-worker

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.6.8
@angular-devkit/build-angular      0.6.8
@angular-devkit/build-ng-packagr   0.6.8
@angular-devkit/build-optimizer    0.6.8
@angular-devkit/core               0.6.8
@angular-devkit/schematics         0.6.8
@angular/cdk                       6.2.1
@angular/cli                       6.0.8
@angular/flex-layout               6.0.0-beta.16
@angular/material                  6.2.1
@angular/pwa                       0.6.8
@ngtools/json-schema               1.1.0
@ngtools/webpack                   6.0.8
@schematics/angular                0.6.8
@schematics/update                 0.6.8
ng-packagr                         3.0.2
rxjs                               6.2.1
typescript                         2.7.2
webpack                            4.8.3

Browser:
[x] Chrome - Version 67.0.3396.87 (Official Build) (64-bit)

Activity

  1. added
    area: core
    on Jun 21, 2018
  2. added this to the needsTriage milestone on Jun 21, 2018
  3. Toxicable commented on Jun 22, 2018

    @Toxicable

    When binding to a function (a getter is a function) Angular must execute the function to get it's result to see if it's changed.
    Therefore everytime Angular runs Change Detection it must execute your function.
    Change Detection can be run very frequently in some cases which can casue this to be a very costly binding.

    When binding to a object angular can do a check very fast since it dosen't need to execute any functions.

    My suggestion: never bind to functions if it can be avoided.

    Angular cant check your template to see if you're binding to a function since it is still a valid case even if it isn't recommended.

  4. un33k commented on Jun 24, 2018

    @un33k
    Author

    @Toxicable It would be nice to warn the users programmatically, the least. Perhaps the change detection can be enhanced to also detect a tight loop and throw an error.

    Like, oh, I have been zipping through this loop 50 thousands times in the past seconds, better give back the control.

    Of course, easier said than done.

  5. Toxicable commented on Jun 25, 2018

    @Toxicable

    @un33k As you mention this is definately easier said than done.
    In some cases binding to a function like

    getFormField() {
      return this.form.controls.person.controls.age.value
    }
    

    This wouldn't casue the same issues that you've seen since it's a very low cost function.
    So it would be impossible for Angular to differentiate this tight loop vs your tight loop.

    The only way I could think would be if Angular was to time function execution time and it is above a threshold then displaya warning.
    This could be done in dev mode only.
    However im unsure of the performance impact this might have, the memory impact would be minimal.

    Proposal

    If devmode
    If binding type === function -> time function execution time.
    if execution time exceeds X time then display a warning:

    Execution time of bound function: ${function ref} exceeded X time, this could casue signifiicant detrimental impact to your app performance. See ${docs link} for more info.

    It would take some investigation to figure out a reasonable execution time threshold
    And the timers accuracy would have to be high since this function in the OP would easily be < 1ms to execute.

  6. dawidgarus commented on Jun 25, 2018

    @dawidgarus

    Shouldn't it throw Expression has changed after it was checked? this.someList returns different object every time.

  7. Toxicable commented on Jun 25, 2018

    @Toxicable

    @dawidgarus Expression has changed after it was checked is for when a value has changed during change detection, that is different from the topic here

  8. dawidgarus commented on Jun 25, 2018

    @dawidgarus

    @Toxicable But it changes during change detection - the object is different in the second pass.

  9. Toxicable commented on Jun 25, 2018

    @Toxicable

    @dawidgarus I don't follow you, what's changing during change detection?
    In the OP this was the example:

    get someList() {
        return [
          { title: 'foo' },
          { title: 'bar  },
        ];
      }
    

    While the reference changes which is fine (but slow) it's value does not change, therefore it is not Expression has changed after it was checked since it's value does not change during Change Detection

  10. mlc-mlapis commented on Jun 25, 2018

    @mlc-mlapis
    Contributor

    ... deep comparison is not done on objects (dev & prod) but is done on arrays (only dev, not prod). The reference change (for objects & arrays) is another topic.

    So big arrays could be a problem in dev mode from the performance point of view.

  11. mhevery commented on Jun 28, 2018

    @mhevery
    Contributor

    Angular works on comparing references not shapes of objects. So every time your getter runs it returns a new value hence the error.

    This works as expected, closing.

  12. angular-automatic-lock-bot commented on Sep 14, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  13. locked and limited conversation to collaborators on Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: core

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions