Smooth long lists

Performance Mid-level Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Pick the fix

Each feed row calls items.sortedBy { it.date } and formats dates inside the composable. What's the best fix?

Options
Hint 1 of 1

How often does this work run?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Sort and format once in the ViewModel, and pass ready UI models to rows

Correct. Work happens once per data change, not per recomposition.

Why not the others?
  • B Wrap each row in remember. Rows still recompute when their inputs change, and it doesn't move the sort.
  • C Use a bigger thread pool. Composition runs on the main thread.
  • D Disable animations. Unrelated.

30-second answer

Give items stable keys and content types so the list reuses compositions, keep item composables cheap (no sorting or formatting during composition), load images at the displayed size with a caching loader, avoid reading fast-changing state in item bodies, and precompute UI models in the ViewModel. Then measure with a release build and FrameTimingMetric.

Practise saying it · 35 s. No recording, just a timer

Explain how to make long lists smooth.

Say your answer out loud, then check which of these points you covered:

  • Keys and content types
  • Cheap item composables
  • Right-size images
  • Precompute in the ViewModel
  • Measure release builds

Deep dive

In Compose, prefetch is built in; nested scrolling lists need fixed sizes or LazyColumn items, not nested lazy lists in the same direction.

Formatting dates and prices in the ViewModel avoids per-frame allocation.

Debug builds of Compose are much slower; never judge scrolling there.

In RecyclerView, use DiffUtil and avoid notifyDataSetChanged.

Common mistakes

  • Sorting or filtering inside the composable.
  • Loading full-size images into thumbnails.
  • Judging scroll performance on a debug build.

Senior take & follow-ups

Give a checklist in priority order, and say how you'd measure.

Follow-up questions

Think of your answer first, then open the hint.

  1. What does contentType do?

    Show hint

    Lets the lazy list reuse compositions between items of the same type.

  2. Where should formatting happen?

    Show hint

    In the ViewModel or mapper, once per data change.

Compare on iOS: Smooth scrolling in lists The same checklist, with cell reuse instead of compositions.

Suggest a fix if something here is wrong or out of date.