Performance benefits with Styles

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.

Phases of Compose and where Styles
run
Figure 1. Phases of Compose and where Styles run.

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

basic_box_border_change

Toggles the border color of a Box to measure update performance.

-59.91%

-77.22%

input_state_basic_box

Compares style-based hover/focus/press states vs. manual interaction state collection.

-5.24%

-14.72%

basic_box

Measures initial composition and layout of a Box with five chained modifiers.

-4.78%

-6.60%

basic_text

Renders five BasicText components with hardcoded strings.

+0.62%

+2.41%

basic_text_provided_color

Compares setting text color via a style vs. using CompositionLocalProvider.

+5.86%

+9.82%