Optimizing Code

Learn how to optimize the code for Ionic applications.

Nothing makes an application run faster than an optimized codebase. Let’s look at some things we can do to improve how our Ionic/Angular code performs.

Unsubscribe from Observables

If we’re managing asynchronous code using Observables, there’s always the danger of a memory leak if we don’t unsubscribe from them correctly.

The problem is this: if a component is destroyed without the subscription to a custom Observable being destroyed, then those subscriptions remain active and can not only cause memory leaks, but may also influence application logic where unintended.

If we’re using the async pipe within a component view, then unsubscribing from the Observable is handled automatically.

Similarly, if we’re subscribing to Observables using Angular’s HttpClient methods such as get() and put(), these are automatically closed after the first value has been emitted in the Observable stream.

When handling custom Observables, though, it’s good practice to manually unsubscribe from them within our code using the ngOnDestroy lifecycle method and the ...