forEach
with ranges KT-P1000Using 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.
Instead of using forEach
on a range like this:
(1..10).forEach {
println(it)
}
Just use a for
loop with the range:
for (i in 1..10) {
println(i)
}
For more details and benchmarks, refer to this article.