By design, Styles operate in the layout and drawing phase of Compose. This avoids the need to create lambda-based modifiers as Styles always skips the composition phase.
The performance improvements over modifiers come from three primary optimizations:
- Phase shifting: Styles often target the Draw phase. When a value changes, Compose invalidates only the affected phase (e.g., Redraw) instead of triggering a full Recomposition or Relayout.
- Lazy allocation: Styles defer animation resource allocation until an animation actually starts. This reduces the work required during initial composition.
- Reduced object overhead: Chained modifiers allocate an object for every property (e.g., padding, border). Styles use a single lambda to apply multiple properties, significantly reducing memory allocations. If a Style is defined in a theme, that lambda is shared across all components using that theme.
The following table shows illustrative results of an internal performance benchmarks for Compose 1.11.0-alpha06 of Styles, in comparison to an implementation in Compose without Styles.
The
basic_box_border_change test highlights the style system's strength in
avoiding the allocation of multiple modifier objects during property updates,
resulting in a massive ~77% reduction in allocations, and ~59% reduction in
time.
Test Method |
Description |
Time Change |
Allocation Change |
Toggles the border color of a |
-59.91% |
-77.22% |
|
Compares style-based hover/focus/press states vs. manual interaction state collection. |
-5.24% |
-14.72% |
|
Measures initial composition and layout of a |
-4.78% |
-6.60% |
|
Renders five |
+0.62% |
+2.41% |
|
Compares setting text color via a style vs. using |
+5.86% |
+9.82% |