question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

perf: run logic outside ngZone

See original GitHub issue

I’m submitting a … (check one with “x”)

  • bug report - search github for a similar issue or PR before submitting
  • feature request
  • support request - use StackOverflow (add the ngx-charts tag) or the gitter chat for support questions

Current behavior The mouseOver event runs inside the ngZone, which leads to hundreds of Angular check which impacts performance.

Expected behavior Some events must run outside the ngZone, only template changes should run inside the ngZone.

Reproduction of the problem https://stackblitz.com/edit/angular-ks78z1?file=src%2Fapp%2Fapp.component.html

What is the motivation / use case for changing the behavior? Perf matters.

  • ngx-charts version: 9.0.0

  • Angular version: 6.1.1

  • Browser: N/A

  • Language: N/A

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Ploppy3commented, Aug 6, 2018

The event is not the problem, the problem is that it runs in the zone. One solution could be to register the event with regular js:

private showToolTip = false;

constructor(
  private ngZone: NgZone
) { }

myFunction(){
  ngZone.runOutsideAngular(() => {
      node.addEventListener('mouseover', () => {
        // compute the new state of the tooltip
        let new_showToolTip = ...
        // detect if the state changes
        if(new_showToolTip != this.showToolTip ){
          // if it does, run inside ngZone, this will trigger change detection
          ngZone.run(() => {
            this.showToolTip = new_showToolTip;
          })
        } // if not, it will not trigger change detection
      })
  })
}

I don’t think there is a memory impact since no additional variables are used.

Even though the computation is a tiny bit more heavy, in the end you skip hundreds of change detection cycles.

0reactions
sebastiangugcommented, Jul 10, 2019

@Ploppy3 @4ctarus https://github.com/swimlane/ngx-charts/issues/1162#issuecomment-510093587

workaround – I’ll be making a pull request from ngx-charts-extra later this month, need to further test possible side-effects as it’s a major change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running event listeners outside of the NgZone - InDepth.Dev
NgZone notifies Angular when to perform the change detection process (e.g. a DOM event with bound listener is one of the triggerers).
Read more >
Angular - ngzone logic for inside vs outside - signalr sample
I have a simple application in angular >= 2 that uses signalr. I have a problem thou with understanding how to determine weather...
Read more >
Zone.js and NgZone in Angular | Run your code outside Angular
Zone.js is responsible for the change detection in angular, angular has NgZone service which helps angular to automatically run the change ...
Read more >
Adding Event Listeners outside of the NgZone - iFour Technolab
When we click the button, both the bound event listener and the change detection process are triggered. In a real-world scenario, instead of...
Read more >
Using Zones in Angular for better performance
NgZone enables us to explicitly run certain code outside Angular's Zone, preventing Angular to run any change detection. So basically, handlers ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found