April 27, 2026
Modifier.composed{} Was Costing Me More Than I Thought. Modifier.Node Fixed It.
A few months back, I had a `Modifier.shimmer()` running on items in a `LazyColumn`. The list had about 30 rows, each with a placeholder…

By Christophy Barth
7 min read
A few months back, I had a Modifier.shimmer() running on items in a LazyColumn. The list had about 30 rows, each with a placeholder card while the data loaded. On paper, fine. In practice, the scroll was rough on a Pixel 4a, and the trace told a story I didn't want to hear: my custom modifier was allocating like crazy on every recomposition.
I'd written it the way every blog post in 2022 told me to. Modifier.composed { … }. Stack a few of these per item, multiply by thirty rows, and you have a per-frame allocation party.
That was the day I finally read up on Modifier.Node, the runtime-side primitive that's been the recommended way to write custom modifiers for a couple of years now. Modifier.composed{} got soft-deprecated in 1.7. It still works and still ships with Compose, but it's the slow path, and most custom modifiers I see in code reviews are still using it.
This is the migration walkthrough I wish I'd had on that profiling session.
What composed{} actually does
The mental shortcut for composed{} is "wraps a composable lambda inside a modifier." That's true, and it's also exactly the problem.
When you write:
fun Modifier.shimmer(): Modifier = composed {
val transition = rememberInfiniteTransition(label = "shimmer")
val offset by transition.animateFloat(
initialValue = 0f,
targetValue = 1000f,
animationSpec = infiniteRepeatable(
animation = tween(1200, easing = LinearEasing)
),
label = "offset"
)
drawBehind {
// draw a moving gradient using `offset`
}
}fun Modifier.shimmer(): Modifier = composed {
val transition = rememberInfiniteTransition(label = "shimmer")
val offset by transition.animateFloat(
initialValue = 0f,
targetValue = 1000f,
animationSpec = infiniteRepeatable(
animation = tween(1200, easing = LinearEasing)
),
label = "offset"
)
drawBehind {
// draw a moving gradient using `offset`
}
}A few things happen on every recomposition where this modifier is used.
A new Modifier instance is materialized for the call site. The lambda runs inside the composition, so remember and the animate* hooks fire as composable nodes in the slot table. The runtime tracks each call site as its own composition, with its own subtree.
Stack three of these on a single list item (clickable, shimmer, custom border), and a fast-scrolling list creates and discards a lot of objects per frame. The clickable case is actually the famous one. The Compose team called it out as a measurable allocation hotspot when it was still backed by composed{}. They rewrote it as a Modifier.Node. Per-frame allocations went away.
So composed{} was always meant as the easy on-ramp for writing custom modifiers without learning a deeper API. It got most of us from zero to working, but the cost was hidden. It only shows up under load, in lists, or when the same modifier ends up applied to a lot of nodes.
What Modifier.Node is, in one sentence
Modifier.Node is a long-lived object that gets attached to a layout node once and updated in place when its inputs change. No allocation per recomposition. No composition machinery for the modifier itself. Direct access to layout, draw, semantics, focus, pointer input, and the rest of the system from one stable instance.
If composed{} is "wrap a composable around this," Modifier.Node is "mount one object onto the layout tree and tell it when its inputs changed." The second one is dramatically cheaper.
The migration, side by side
Let's take a small example so the contract is visible. A Modifier.fadeOnScroll(scrollState) that fades a child as you scroll past a threshold.
The old way:
fun Modifier.fadeOnScroll(scrollState: ScrollState): Modifier = composed {
val alpha = (1f - scrollState.value / 600f).coerceIn(0f, 1f)
graphicsLayer { this.alpha = alpha }
}fun Modifier.fadeOnScroll(scrollState: ScrollState): Modifier = composed {
val alpha = (1f - scrollState.value / 600f).coerceIn(0f, 1f)
graphicsLayer { this.alpha = alpha }
}Every time scrollState.value changes (every scroll frame, basically), the whole composed lambda re-runs, builds a new modifier chain, and replaces the old one.
The Modifier.Node version has more pieces but pays for itself.
private data class FadeOnScrollElement(
val scrollState: ScrollState
) : ModifierNodeElement<FadeOnScrollNode>() {
override fun create(): FadeOnScrollNode =
FadeOnScrollNode(scrollState)
override fun update(node: FadeOnScrollNode) {
node.scrollState = scrollState
}
override fun InspectorInfo.inspectableProperties() {
name = "fadeOnScroll"
properties["scrollState"] = scrollState
}
}
private class FadeOnScrollNode(
var scrollState: ScrollState
) : Modifier.Node(), LayoutModifierNode {
override fun MeasureScope.measure(
measurable: Measurable,
constraints: Constraints
): MeasureResult {
val placeable = measurable.measure(constraints)
return layout(placeable.width, placeable.height) {
placeable.placeWithLayer(0, 0) {
// state read happens here, inside the layer block,
// so only the layer re-records on scroll. No remeasure.
alpha = (1f - scrollState.value / 600f).coerceIn(0f, 1f)
}
}
}
}
fun Modifier.fadeOnScroll(scrollState: ScrollState): Modifier =
this then FadeOnScrollElement(scrollState)private data class FadeOnScrollElement(
val scrollState: ScrollState
) : ModifierNodeElement<FadeOnScrollNode>() {
override fun create(): FadeOnScrollNode =
FadeOnScrollNode(scrollState)
override fun update(node: FadeOnScrollNode) {
node.scrollState = scrollState
}
override fun InspectorInfo.inspectableProperties() {
name = "fadeOnScroll"
properties["scrollState"] = scrollState
}
}
private class FadeOnScrollNode(
var scrollState: ScrollState
) : Modifier.Node(), LayoutModifierNode {
override fun MeasureScope.measure(
measurable: Measurable,
constraints: Constraints
): MeasureResult {
val placeable = measurable.measure(constraints)
return layout(placeable.width, placeable.height) {
placeable.placeWithLayer(0, 0) {
// state read happens here, inside the layer block,
// so only the layer re-records on scroll. No remeasure.
alpha = (1f - scrollState.value / 600f).coerceIn(0f, 1f)
}
}
}
}
fun Modifier.fadeOnScroll(scrollState: ScrollState): Modifier =
this then FadeOnScrollElement(scrollState)Three things changed.
The element is a data class. That's load-bearing. ModifierNodeElement uses equals and hashCode to decide whether to call create() (new node) or update() (existing node, new inputs). Forget data class, or override equals badly, and you get a fresh node on every parent recomposition. You've now paid more than the composed version did. This is the single most common bug.
The node holds mutable fields and reads them at draw or layout time. Because the node is alive across recompositions, "the latest scrollState" is just node.scrollState. There's no remember. There's no recomposition of the modifier itself. There's a single update() that swaps the field.
The then is what mounts it. this then FadeOnScrollElement(…) is the new chain syntax. No composable scope.
For a small example like this, the line count went from 4 to about 25. That sounds bad. The wins show up under load. On a list of items with three of these in the chain each, the allocation profile in Android Studio's profiler is a different shape entirely. The old version generated thousands of Modifier objects during a one-second fling. The new version generates a handful.
What you actually need to override
Modifier.Node is a small base class. To do anything useful, you implement one or more of these interfaces.
LayoutModifierNode for measure and layout work.
DrawModifierNode for draw(). This is what drawBehind and drawWithContent used to give you.
SemanticsModifierNode for accessibility info.
PointerInputModifierNode for gestures.
FocusEventModifierNode and friends for focus.
ObserverModifierNode if you need to wrap state reads in observeReads { … } and re-invalidate yourself when those reads change.
Most custom visual effects only need DrawModifierNode. Most custom layout tweaks only need LayoutModifierNode. You combine interfaces freely on a single node when the work crosses concerns.
The lifecycle hooks worth knowing
The node has three callbacks that catch most of the cleanup-related bugs.
onAttach() runs when the node is first added to the layout tree. Good place to start coroutines (the node has its own coroutineScope), register listeners, kick off any setup that needs the node attached.
onDetach() runs when the node is removed for real, not on every parent recomposition. This is where you tear down listeners, cancel one-off jobs that the node-scoped coroutineScope doesn't already handle, and release resources.
onReset() runs when the node is being recycled (think LazyColumn reusing the same modifier across items). Reset internal state to defaults so the next attach starts clean.
If you've ever leaked a listener from a DisposableEffect because of a missed key, you'll appreciate that these are tied to the node's actual lifetime, not to a remember slot.
What I got wrong the first time
A few things I tripped on, in case they save you the trip.
I forgot data class on the element. The first version of my FadeOnScrollElement was a regular class. Every parent recomposition produced a new element instance, equals returned false (default object identity), and the runtime called create() instead of update(). I'd built a more-expensive version of composed{}. The fix was a one-word edit.
I passed a lambda as a parameter and watched my equals contract fall over. Lambdas don't have structural equality. If your element holds an onClick: () -> Unit and the parent passes { doThing() }, every recomposition produces a new lambda reference, equals returns false, and you're back to the same bug. The fix is to either capture lambdas via rememberUpdatedState and pass a stable reference, or store them in the node as a var and accept that the equals check ignores them.
I treated update() like a constructor. update() should mutate the node's existing fields, not rebuild internal state. If the only thing that changed is one input, set that one field. The runtime figures out what needs invalidation based on which interfaces the node implements.
I cached a derived value in the node and forgot to invalidate it when the inputs changed. State reads happening inside measure(), draw(), or a layer block are tracked automatically. State reads happening inside helpers you call from a coroutine, or values cached into a regular var "for performance," are not. If you cache derived values, call invalidateDraw() (or invalidateMeasurement()) when the underlying inputs shift.
When you'd still reach for composed{}
Honestly, rarely on a fresh codebase. The cases where composed{} is the right tool are narrow.
You need a composable hook (remember, LaunchedEffect, theming, locals) inside the modifier, and the work is genuinely composition-bound.
You're prototyping, and the modifier will live for two days.
The modifier is applied to one specific composable in the whole app and is not on a hot path.
For everything else, Modifier.Node is the better default. The boilerplate is real but constant. You write it once per modifier, not once per use.
Where to start if you've got a pile of these
The instinct is "rewrite all of them." Don't. The whole point of the composed{} slow path is that it's still correct, just costlier. Profile first, fix the worst.
The pattern I follow:
- Run a list-heavy screen with the profiler. Look at the allocations tab during a fast scroll.
- Sort by class count. Custom modifiers and lambdas show up. So do
Modifier.CombinedModifierinstances. - Pick the one with the highest count. That's the modifier to rewrite first.
- Write the
ModifierNodeElementandModifier.Nodepair. Make the element adata class. - Re-profile. Confirm the count dropped. Move to the next one.
Three or four migrations later, the worst hotspots are gone. The rest of your composed{} modifiers are usually fine to leave in place until they prove themselves a problem.
You don't have to write these yourself
Or you can skip a chunk of step 4 entirely. I open-sourced the whole pile on GitHub at ChristophyBarth/modifier-node-cookbook, Apache 2.0, 20 modifiers and 2 composites, all following the rules above: a node, an update(), no composed{} anywhere. Go read the source, file an issue if something feels off, or send a PR if there's a modifier you keep rewriting that should be in the cookbook. Stars don't hurt either 🙃.
The takeaway
composed{} was the easy door into custom modifiers. Modifier.Node is the door the framework wants you to walk through now. The cost is some boilerplate and the discipline to make the element a data class. The win is real, measurable, and shows up exactly where you wanted it. In the profile of your worst-case list.
If you've got a custom shimmer, a fade, a clickable variant, a custom border, or any modifier that ends up on a lot of nodes, that's the migration to do this week. Pick the loudest one in the profiler, rewrite it, and watch the allocation count fall.
May your modifiers be nodes and your allocation counts stay low.