Files
aiturk-mobil/app/src/test/java/com/m57/hermescontrol/NavigationBenchmarkTest.kt
T
M57 045932ca8b Optimize collection creation in Navigation.kt (#180)
* ⚡ Optimize collection creation in Navigation.kt

Replaced uses of `.map { ... }.toSet()` with `.mapTo(mutableSetOf()) { ... }` in Navigation.kt. This avoids the creation of intermediate ArrayList objects, saving unnecessary memory allocations.

* ⚡ Optimize collection creation in Navigation.kt

Replaced uses of `.map { ... }.toSet()` with `.mapTo(mutableSetOf()) { ... }` in Navigation.kt. This avoids the creation of intermediate ArrayList objects, saving unnecessary memory allocations. Fixed ktlint formatting issues causing CI failure.
2026-06-20 23:44:39 +03:00

39 lines
1.0 KiB
Kotlin

package com.m57.hermescontrol
import org.junit.Test
import kotlin.system.measureNanoTime
class NavigationBenchmarkTest {
@Test
fun benchmarkMapToSet() {
// Simulate ALL_NAV_ITEMS which has 20 items
val items = (1..20).toList()
// Warmup
for (i in 1..10000) {
val a = items.map { it }.toSet()
val b = items.mapTo(mutableSetOf()) { it }
}
var timeMapToSet: Long = 0
var timeMapTo: Long = 0
val iterations = 100000
for (i in 1..iterations) {
timeMapToSet +=
measureNanoTime {
items.map { it }.toSet()
}
timeMapTo +=
measureNanoTime {
items.mapTo(mutableSetOf()) { it }
}
}
println("============================")
println("Baseline (.map.toSet): ${timeMapToSet / iterations} ns/op")
println("Optimized (.mapTo): ${timeMapTo / iterations} ns/op")
println("============================")
}
}