Pattern: Use of forEach
on range
Issue: -
Using the forEach
method on ranges has a heavy performance cost. Prefer using simple for
loops.
Benchmarks have shown that using forEach on a range can have a huge performance cost in comparison to simple for loops. Hence in most contexts a simple for loop should be used instead.
Example of incorrect code:
(1..10).forEach {
println(it)
}
(1 until 10).forEach {
println(it)
}
(10 downTo 1).forEach {
println(it)
}
Example of correct code:
for (i in 1..10) {
println(i)
}