apexcharts / ng-apexcharts

ng-apexcharts is an implementation of apexcharts for angular. It comes with one simple component that enables you to use apexcharts in an angular project.
MIT License
310 stars 78 forks source link

Improve performance by running outside of ngZone #198

Closed DavidMarquezF closed 2 years ago

DavidMarquezF commented 2 years ago

In order to do the "magic" of change detection, angular uses zone.js. This patches a lot of functions, like hover, requestAnimationFrame and others and tells angular to rerun changeDetection.

Right now the chart runs inside the angular zone which means that all requestAnimationFrame, hover for tooltips, clicks, etc. will trigger change detection. I don't think they should as this causes a big hit in performance for something that is not really needed since apexchart.js already runs without angular intervention.

To get an idea, see the console output, which says how many change detection cycles have ran:

Prior to this change, pressing the button add series: Screenshot_3 After this change: Screenshot_1

To solve this I run the rendering of the chart outside of the angular zone which means that it will not detect these calls.

However I am not sure of the following:

junedchhipa commented 2 years ago

Hey, thanks for your contribution!

To answer your first question, yes, updateSeries, addSeries in ApexCharts context trigger a re-render themselves. And to answer your second question, we need to only know if the reference provided to the chart component changed to trigger a re-render.

DavidMarquezF commented 2 years ago

Perfect, then this pull-request should be complete now. We now run all the functions outside the angular zone and since we only care about an input reference change, I set the strategy to OnPush. In theory everything should still work without any issues and with much better performance, closer to the one of the native apexcharts.js

DavidMarquezF commented 2 years ago

Any idea of when this could be released @junedchhipa ?