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 Sep 22, 2024
1 parent 74f9198 commit 4eb1548
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 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 @@ -24,7 +24,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
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.koin.core.instance

import org.junit.Assert.*
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.fail
import org.junit.Test
import org.koin.core.error.InstanceCreationException
import org.koin.core.logger.Level
Expand Down Expand Up @@ -123,7 +125,7 @@ class CreateAPITest {
)
}.koin

(1..3).forEach {
for (index in 1..3) {
koin.get<Component>()
}
}
Expand Down

0 comments on commit 4eb1548

Please sign in to comment.