Repository navigation
Closed
Labels
area: coreIssues related to the framework runtime
Milestone
Description
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>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)
Reacted by Максим Иванов, Prabhdeep Singh Batth and Marius Göcke
Reacted by Trotyl Yu
Metadata
Metadata
Assignees
Labels
area: coreIssues related to the framework runtime

Activity
Toxicable commentedon Jun 22, 2018
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.
un33k commentedon Jun 24, 2018
@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.
Toxicable commentedon Jun 25, 2018
@un33k As you mention this is definately easier said than done.
In some cases binding to a function like
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:
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.
dawidgarus commentedon Jun 25, 2018
Shouldn't it throw
Expression has changed after it was checked?this.someListreturns different object every time.Toxicable commentedon Jun 25, 2018
@dawidgarus
Expression has changed after it was checkedis for when a value has changed during change detection, that is different from the topic heredawidgarus commentedon Jun 25, 2018
@Toxicable But it changes during change detection - the object is different in the second pass.
Toxicable commentedon Jun 25, 2018
@dawidgarus I don't follow you, what's changing during change detection?
In the OP this was the example:
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 checkedsince it's value does not change during Change Detectionmlc-mlapis commentedon Jun 25, 2018
... 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.
mhevery commentedon Jun 28, 2018
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.
angular-automatic-lock-bot commentedon Sep 14, 2019
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.