4
1

More than 3 years have passed since last update.

Ionic with Zone.js

Last updated at Posted at 2019-12-18

この記事は Ionic Advent Calendar 2019 20日目の記事です。

Ionic with Zone.js

この記事は前日のMeetupで発表したものです。

Ionic4 + Zone.js 0.9.1 でのパフォーマンス低下

Ionic4 (Angular)でZone.js 0.9.0から0.9.1にアップグレードしたあとで、動きが遅くなりました。調査したところ、Change Detectionの回数が多くなっていたことがわかりました。その理由を説明させていただきます。

Ionic 4 の仕組み

Ionicはバージョン4からStencilを利用して、すべてがWebComponentになりました。

Screenshot from 2019-12-19 00-09-43.png

そして、Zone.jsは0.9.1からWebComponentのAPIを全部monkey patchしました。下記のようなイメージです。

Screenshot from 2019-12-19 00-35-17.png

その結果、WebComponentがangular zoneに自動的に入りました。このPatchはWebComponent用ですが、Angular Application用ではありません。

しかし、Ionicの場合、すべてのComponentはWebComponentであると同時に、普通のAngular Applicationでもあります。つまり、IonicのComponentが普通のAngular ApplicationのComponentとしてラップされているため、すでにangular zoneの中に入っています。

その結果、なにかInputが変更する場合、

  • Angular ApplicationによるAngular ZoneのChange Detection
  • Zone.js PatchされたCustom Elements APIのChange Detection

両方がTriggerされ、パフォーマンスが低下します。

解決案

解決方法はZone.jsのPatchを無効化することです。

やりかたとしては、一つのzone-flag.tsをつくって、

zone-flag.ts
(window as any).__Zone_disable_customElements = true;

をいれます。

そして、polyfilles.ts

polyfilles.ts
import './zone-flag';
import 'zone.js/dist/zone'; // Included with Angular CLI.

をいれて、zone.jsのCustomElements APIのPatchを無効化します。

以上です。
どうもありがとうございました!

4
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1