Skip to content

Files

Latest commit

 

History

History
36 lines (26 loc) · 854 Bytes

ForEachOnRange.md

File metadata and controls

36 lines (26 loc) · 854 Bytes

Pattern: Use of forEach on range

Issue: -

Description

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)
}

Further Reading