Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 37 ยท Testing and Performance

Performance and Lazy Loading

Make Ionic apps start fast and scroll smoothly: lazy load routes, virtualize long lists, size images, debounce input, avoid layout work and measure with real tools.

A mobile web view runs on a phone that may be four years old, on a weak network, with a battery to protect. Users judge an app in the first two seconds and on the first scroll. Performance work in Ionic comes down to four things: ship less JavaScript, render less DOM, do less work on every event, and measure before changing.

Measure first

Guessing wastes time. Use:

  • Chrome DevTools with a real Android phone attached (chrome://inspect) or Safari Web Inspector for iPhones. The Performance panel shows long tasks and layout work; the Network panel shows bundle size.
  • Lighthouse for a quick report on the web build.
  • Bundle analyzers (source-map-explorer, rollup-plugin-visualizer, Angular's --stats-json with a viewer) to see which dependency is large.
  • Throttle CPU 4x or 6x while testing, because your laptop is much faster than a phone.

Lazy load routes

If the app downloads every page on startup, the first screen waits for code it does not need. Lazy loading splits the bundle per route and downloads a page the first time it is visited. Real routes, read-only:

TypeScript
// Angular standalone routes
export const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', loadComponent: () => import('./home/home.page').then(m => m.HomePage) },
  { path: 'photos', loadComponent: () => import('./photos/photos.page').then(m => m.PhotosPage) },
];
TSX
// React: lazy() with Suspense
const Photos = React.lazy(() => import('./pages/Photos'));
<Route path="/photos"><Suspense fallback={<IonSpinner />}><Photos /></Suspense></Route>

Ionic also preloads pages in some setups so that the next tap is instant; Angular's PreloadAllModules strategy downloads lazy routes after the first load. Use it for small apps and a custom strategy for big ones.

JS Lazy loading with a cache

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Long lists

Each list item is DOM: nodes, styles, layout. A list of 5,000 items can freeze a phone. Virtual scrolling renders only the items near the viewport and reuses them as you scroll. Ionic's ion-content combined with the framework tools does this: Angular CDK's <cdk-virtual-scroll-viewport> (Ionic's own ion-virtual-scroll was removed in v7), react-window or react-virtuoso in React, vue-virtual-scroller in Vue.

The idea is plain arithmetic. With a fixed row height, the first visible index is scrollTop / rowHeight, and you render a few rows above and below as a buffer.

JS The math of virtual scrolling

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The total scroll height is kept with spacers so that the scrollbar stays correct. Variable row heights are harder; libraries measure and cache them. For lists up to a few hundred simple items, plain rendering with trackBy (Angular) or stable key props (React) is enough. Also consider infinite scroll (ion-infinite-scroll) to load pages of 20 to 50 items.

Images

Images are usually the biggest bytes on a screen.

  • Use ion-img or loading="lazy" so off-screen images are not fetched.
  • Resize on the server or when saving: do not show a 4000 px photo in a 100 px thumbnail. The Camera plugin's width option helps (Camera and Filesystem).
  • Prefer modern formats such as WebP or AVIF for remote images, and SVG for icons.
  • Always set width and height (or aspect ratio) to avoid layout jumps.

Work per event

Scroll, input and resize events fire many times per second. Do the minimum inside them: debounce typing before searching, throttle scroll handlers, and prefer IntersectionObserver to scroll listeners.

JS Debounce and throttle

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Rendering and animation

  • Animate transform and opacity, which the GPU handles, not height or top.
  • Avoid reading layout (offsetHeight) and writing styles in a loop; it forces repeated layout.
  • In Angular, use ChangeDetectionStrategy.OnPush with signals or immutable data; in React, memoize expensive children and keep state close to where it is used.
  • Remove listeners and subscriptions on leave, since Ionic keeps pages alive (State Management in Ionic Apps).

Startup matters most. Keep the splash screen up until the first page is ready, load nonessential plugins and analytics after the first render, and cache the last known data locally so the first screen has content immediately (Offline First and Sync).

flowchart LR
  A["Measure<br/>(real device, throttled)"] --> B["Find the biggest cost"]
  B --> C["Fix one thing"]
  C --> D["Measure again"]
  D -->|"Better?"| E["Keep"]
  D -->|"No"| F["Revert"]

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does lazy loading a route achieve?

    Splitting by route shrinks the initial bundle and speeds up startup.
  2. 2. How many rows does a virtual scroll list keep in the DOM?

    Rows are created and reused as the scroll position changes, with spacers keeping the scroll height.
  3. 3. Which CSS properties are cheapest to animate?

    Transform and opacity avoid layout and can be composited by the GPU.
  4. 4. Why debounce a search input?

    Debouncing collapses bursts of events into one call.

Summary

  • Measure on a throttled, real device before optimizing.
  • Lazy load routes and heavy libraries; preload what the user will likely visit next.
  • Virtualize long lists and paginate remote data.
  • Size and lazy load images, debounce or throttle frequent events.
  • Animate transform and opacity, clean up listeners, and keep startup light.

Next: Building for Android and iOS