1

I'm using a Jquery plugin in my angular2 project. It has a serious performance problem if the plugin has an event like $(window).mousemove() or setInterval(function(){}, 10). Because zone.js has hooks to html events, the page always checks data changes. My cpu is running very high.

Milad
  • 27,506
  • 11
  • 76
  • 85
Moon
  • 259
  • 1
  • 2
  • 12
  • Read about OnPush – Eran Shabi Dec 06 '16 at 07:24
  • Possible duplicate of [Prevent a native browser event ( like scroll ) from firing change detection](http://stackoverflow.com/questions/37315781/prevent-a-native-browser-event-like-scroll-from-firing-change-detection) – Milad Dec 06 '16 at 09:02

2 Answers2

1

you can use

@HostListener('mousemove', ['$event'])
  onMousemove(event: MouseEvent) { this.mousemove.emit(event); } 

instead $(window).mousemove()

will increase your performance

anshuVersatile
  • 2,030
  • 1
  • 11
  • 18
1

You can use NgZone to make code run inside or outside of Angulars zone.

Outside for performance reasons, inside to ensure Angular gets notified that change detection has to be run when properties of a component, model, or service are modiefied:

constructor(private zone:NgZone) {
  zone.runOutsideAngular(() => {
    $(window).mousemove()...;
    setInterval(() => {}, 10);
  })
}

onMouseMove() {
  this.zone.run(() => {
    this.someProp = 'someValue';
    this.router.navigate(....)
  });
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • ngZone.runOutsideAngular is working doCheck call once. but if the route jumps doCheck always call. – Moon Dec 06 '16 at 07:58
  • What do you mean by "if the route jumps doCheck"? – Günter Zöchbauer Dec 06 '16 at 08:00
  • I don't get how routing is related to your question. – Günter Zöchbauer Dec 06 '16 at 08:02
  • from router '/a' to router '/b' runOutsideAngular is not work. refresh '/b' runOutsideAngular is work – Moon Dec 06 '16 at 08:04
  • That's why I added ` this.router.navigate(....)` as example inside `this.zone.run()`. `router.navigate()` depends on change detection to happen. With `runOutsideAngular()` updating the view won't work. – Günter Zöchbauer Dec 06 '16 at 08:07
  • Maybe I have a mistake description :(. I mean that if I signal access /b, runOutsideAngular works. But if I jump from '/a'(another page) to '/b', then runOutsideAngular doesn't work. I don't know how to fix this issue. – Moon Dec 06 '16 at 08:17
  • I don't know how you use `runOutsideZone` or how you jump from `/a` to `/b`. – Günter Zöchbauer Dec 06 '16 at 08:18
  • Component template [routerLink]="'/b"' .... class BComponent(){ constructor(private ngZone: NgZone){ } ngAfterViewInit(){ this.ngZone.runOutsideAngular(()=>{ echarts.init() }) } } I access page b via http://xxxxxx/#/b, then runOutsideAngular works. If I jump from page a (via click a link) to page b, runOutsideAngular doesn't work. :( – Moon Dec 06 '16 at 08:29
  • Can you reproduce in a Plunker? (Plunker has an Angular2 template). – Günter Zöchbauer Dec 06 '16 at 08:31