A benchmarking investigation into whether Kotlin Sequences are faster than standard Collections for chained operations like filter and map. Using kotlinx-benchmark on JDK 21, the results show Sequences are actually slower — about 9% slower for simple chains and up to 45% slower for complex chains with many intermediate operations. Surprisingly, Kotlin Flow outperforms Sequences in the extreme case. Additional benchmarks with ImmutableArray (from Pods4k) and large datasets (100,000 items) reinforce that standard Collections and ImmutableArrays are generally the fastest choice. The key takeaway: Sequences incur per-element function call overhead that outweighs the savings from avoiding intermediate collections in most practical scenarios.

6m read timeFrom chrisbanes.me
Post cover image
Table of contents
Collections #Sequences #Flow #Applying transformations #Benchmark #Benchmark Results: Sequences can be slow #Lessons Learnt #Eating humble pie 🥧 #Update 1: Using Immutable collections #Update 2: Large data set #
1 Impression