components icon indicating copy to clipboard operation
components copied to clipboard

Performance Improvement For Mat-Menu And Mat-Table Sort Header

Open surajtambe opened this issue 7 years ago • 11 comments

I have created one common table component, within it i am using mat-menu to provide actions on row. i was optimizing the component during which i found some bugs. When we hover on matmenu it trigger changedetection in angular. same happening with table header hover as well.

I think change detection is not neccessary on hover, as for huge table on mobile devices it creates lag.

In Below demo example once we open console we can check changedetection is triggered.

Demo URL : https://stackblitz.com/edit/angular-uyqypq

surajtambe avatar Oct 07 '19 04:10 surajtambe

We have good reasons for both of these:

  1. The menu needs to do it in order to toggle the highlighted state. This is used with nested menus.
  2. The sort header needs to trigger it for its animations to work. We can work around this, but it'll require some refactoring.

crisbeto avatar Oct 07 '19 19:10 crisbeto

@crisbeto : Thank you for quick response.

It would have been great feature( performace Improvement. ) to normalize hover events as for larger pages.

  1. Is it posible to disable same for simple menu ?
  2. For sort header, this will create problem for tables with 100 rows, and If some computation involved, based on user permissions / status of row , it will definitely hamper perfomance.

can you suggest any workaround that I can apply externally ?

surajtambe avatar Oct 08 '19 11:10 surajtambe

  1. I was thinking about doing that, but we'd have to check if any of the items can trigger a sub-menu. This could end up being annoying because the menu associated with a trigger can be changed through a data binding.
  2. Just because change detection is running doesn't necessarily mean that your app will slow down. Change detection is usually pretty fast since boils down to a bunch of a === b checks, it's usually what happens as a result of change detection that is slow.

crisbeto avatar Oct 08 '19 18:10 crisbeto

@crisbeto

  1. As simple menu used almost 80 %( rough estimate ) of time in applications. Nested menu is mostly used for special case. If we can identify nested menu using any directive / option paramter we can run change detection for nested menus.
  2. Change Detection is done in order to skip DOM changes. To make application more dynamic developers use *ngFor *ngIf with methods which makes change detection to consume cpu.
  3. For Mat table for If change detection is run on header hover it will consume CPU which is tricky because it creates lag in arrow animation.
  4. If we consider mobile or touch devices for PWA, change detection is costly for such devices if nested components are not implemented and whole page is developed in single component.
  5. Change detection is also heavy for nested *ngFor as well. --> Table Header issue might be realted to below issues #15451, #13012, #11953

surajtambe avatar Oct 11 '19 01:10 surajtambe

@surajtambe Two suggestions

  1. Use a trackBy function for your table too.
  2. Use lazy rendered menus

manklu avatar Nov 04 '19 22:11 manklu

@manklu

@surajtambe Two suggestions

  1. Use a trackBy function for your table too.
  2. Use lazy rendered menus

Thank you.

  1. I implemented lazy rendered menus and it does help redcuce change detection cycles. Above also made me revisit documentation and helped me with mat-expansion-panel lazy loading as well.
  2. I have used trackBy in for columns, it is helpful for efficient change detection.

surajtambe avatar Nov 05 '19 05:11 surajtambe

for the experiment - declare the public render () function with the console log inside and interpolate to the table row <tr>{{render()}}</tr> You will see how many times the rows in the table are redrawn. ChangeDetection doesn't help. trackBy(index, item) {return item.id} doesn't help. trackBy(index, item) {return index} data binding falls off (three columns are sorted, the rest do not change). Mat-table redraws rows at any event such as scroll, hover, display mat-tooltip. Even outside the table itself, if material components such as buttons, inputs are used on the page. Sorry for my English.

evheniyrz avatar Apr 27 '20 12:04 evheniyrz

@evheniyrz Agreed, It is more of an overhead on machine. It would be great if any would can come up with solution . It seems hard fix for many.

surajtambe avatar Apr 28 '20 02:04 surajtambe

@surajtambe It is impossible to solve on their own. I parsed the code into the table js, with the entry on the stack - with one call to sorting in the table header, an incredible number of animation verification cycles in the ng zone are called

evheniyrz avatar Apr 28 '20 04:04 evheniyrz

Closing this since it's low enough priority that I don't see us working on it any time in the near future. At some point we will explore changing the sort header to use native CSS animations, which would probably resolve this for that component at least.

jelbourn avatar Mar 23 '21 22:03 jelbourn

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.