Kotlin

Kotlin

Made by DeepSource

Avoid using forEach with ranges KT-P1000

Performance
Major

Using the forEach method on ranges has a heavy performance cost. Prefer using simple for loops.

Benchmarks have shown that in most contexts, a simple for loop should be used instead.

Bad Practice

Instead of using forEach on a range like this:

(1..10).forEach {
    println(it)
}

Recommended

Just use a for loop with the range:

for (i in 1..10) {
    println(i)
}

For more details and benchmarks, refer to this article.