Jetpack Compose delivers excellent performance out of the box. Configure your app using best practices to avoid common pitfalls and optimize your Compose application's performance.
Key concepts
These are some of the key concepts for performance in Compose:
- Phases: Understanding the composition, layout, and drawing phases is crucial for optimizing how Compose updates your UI.
- Baseline Profiles: These profiles precompile essential code, leading to faster app launches and smoother interactions.
- Stability: Increase the stability of your app to more efficiently skip unnecessary recompositions, improving performance.
Properly configure your app
If your app is performing poorly, there might be a configuration problem. A good first step is to check the following configuration options:
- Build in Release Mode with R8: Try running your app in release mode. Debug mode is useful for spotting many problems, but it imposes a performance cost and can make it hard to spot other issues. You should also enable optimizing and shrinking with the R8 compiler to ensure a performant and efficient release build.
- Use Baseline Profiles: Baseline Profiles improve performance by precompiling code for critical user journeys. Compose includes a default profile, but ideally, you should create an app-specific one as well. Learn more about Baseline Profiles in the general Android performance docs
Tools
Familiarize yourself with the suite of tools available to help you measure and analyze your Compose app's performance.
Best Practices
When developing your app with Compose, keep these best practices in mind:
- Avoid expensive calculations: Use
remember
to cache the results of expensive calculations. - Help lazy layouts: Provide stable keys to lazy layouts using the
key
parameter to minimize unnecessary recompositions. - Limit unnecessary recompositions: Use
derivedStateOf
to limit recompositions when rapidly changing state. - Defer state reads: Defer state reads as long as possible by wrapping them in lambda functions.
- Use lambda modifiers for changing state: Use lambda-based
modifiers like
Modifier.offset { ... }
for frequently changing state variables. - Avoid backwards writes: Never write to state that has already been read in a composable.
For more details, see the best practices guide.
Views
If you are working with views instead of Compose, see the dedicated Improve layout performance guide.
Additional Resources
- App performance guide: Discover best practices, libraries, and tools to improve performance on Android.
- Inspect Performance: Inspect app performance.
- Benchmarking: Benchmark app performance.
- App startup: Optimize app startup.
- Baseline profiles: Understand baseline profiles.