Skip to content

Commit

Permalink
[Maintenance] performance forEach on range
Browse files Browse the repository at this point in the history
- `forEach` function [Performs the given operation on each element of this Iterator.](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/for-each.html).
- `for` loop [A for loop over a range or an array is compiled to an index-based loop that does not create an iterator object.](https://kotlinlang.org/docs/control-flow.html#for-loops)

First one will create `Iterator` object, second will not. It was used like that only in tests. But gaining slightly faster tests is good in my opinion.
  • Loading branch information
GrzegorzBobryk committed Oct 27, 2024
1 parent 2eb9163 commit ba22296
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.koin.test

import kotlinx.coroutines.runBlocking
import org.koin.core.*
import org.koin.core.context.GlobalContext.startKoin
import org.koin.core.context.GlobalContext.stopKoin
import org.koin.core.context.waitKoinStart
import org.koin.core.logger.*
import org.koin.core.isAllStartedJobsDone
import org.koin.core.lazyModules
import org.koin.core.logger.EmptyLogger
import org.koin.core.logger.Level
import org.koin.core.logger.Logger
import org.koin.core.onKoinStarted
import org.koin.core.runOnKoinStarted
import org.koin.core.time.Timer
import org.koin.core.waitAllStartJobs
import org.koin.dsl.koinApplication
import org.koin.mp.KoinPlatform
import kotlin.test.Test
Expand All @@ -15,12 +21,16 @@ class PerfsTest {

@Test
fun perfModule400_module_perfs() {
(1..3).forEach { runPerfs() }
for (index in 1..3) {
runPerfs()
}
}

@Test
fun perfModule400_lazy_module_perfs() {
(1..3).forEach { runPerfs(isLazy = true) }
for (index in 1..3) {
runPerfs(isLazy = true)
}
}

private fun runPerfs(log: Logger = EmptyLogger(), isLazy: Boolean = false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ParameterStackTest {
)
}.koin

(1..10).forEach {
for (index in 1..10) {
koin.get<Simple.MyStringFactory> { parametersOf(KoinPlatformTools.generateId()) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DebugLogTest {
printLogger(Level.INFO)
}.koin

(1..10).forEach {
for (index in 1..10) {
measureDuration("with if") {
if (koin.logger.isAt(Level.DEBUG)) {
koin.logger.debug(message { "test me" })
Expand Down

0 comments on commit ba22296

Please sign in to comment.