
It requires one extra object allocation and additional if-checks. Option 2 would seem to be just as good as option 1, but adding the step parameter makes Kotlin use a more complex set of logic that can handle any step value you might throw at it. The same compilation behavior is seen for simple decrementing progressions as well (e.g. It allocates a couple primitives and has a couple if-checks, but is otherwise fairly simple. Given that the compiler can optimize ranges, which of the following range-based for-loops do you think is the most efficient? // Option 1


No allocations necessary! Range Against The Machine In other words, when endInclusive urlThumbĪgain, the above compiles to a series of simple conditionals. That's because IntRange is defined by its minimum start and its maximum endInclusive values.

If you think the answer is 123321, guess again. Pop quiz: What does the following code output? (1.3).forEach(System.out::print) Here are a few interesting aspects of Kotlin ranges, some of which I've found to be less-than-intuitive.
