<https://detekt.dev/docs/rules/performance/#foreac...
# detekt
e
https://detekt.dev/docs/rules/performance/#foreachonrange (and other perf rules) references blog posts that are 7 years old. (Kotlin 1.1) Are they still relevant?
s
The
ForEachOnRange
rule is still relevant. See here for more information regarding the official APIs. https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/for-each.html > But if you take a closer look at the signature of the forEach() function used here, you’ll notice that it’s not optimized for ranges but only for Iterable, so it requires the creation of an iterator. Source: https://bladecoder.medium.com/exploring-kotlins-hidden-costs-part-3-3bf6e0dbf0a4
In general, if a rule becomes obsolete, it will be stated in the docs accordingly.
e
It felt like something that could easily have been optimized under the hood over the course of those 7 years.. but seems like it haven't. 🙂
Copy code
main summary:
Benchmark                  (iterationsPerForEach)   Mode  Cnt          Score          Error  Units
MyBenchmark.forLoop                            10  thrpt    5  449993800,059 ± 26032787,135  ops/s
MyBenchmark.forLoop                           100  thrpt    5  123058934,812 ±   803845,361  ops/s
MyBenchmark.rangedForEach                      10  thrpt    5  212422928,653 ±  2200979,906  ops/s
MyBenchmark.rangedForEach                     100  thrpt    5   20090642,098 ±  1329771,102  ops/s
Pushed the benchmark here https://github.com/Kantis/foreach-bench
👍 1