Home > Back-end >  Jetpack Compose mutableStateList vs mutableList Performance
Jetpack Compose mutableStateList vs mutableList Performance

Time:12-16

How do mutableState, mutableStateList, mutableStateMap perform in comparison to a normal variable, mutableList, mutableMap? If there are observers listening they will be slower of course (because recomposition is triggered) but is there a difference between them if there are no observers at all?

Thanks in advance.

CodePudding user response:

I don't think slower is the right word here, but if any composable is observing that state variable - it will be recomposed. So it might slow your app if it's done too often.

You cannot compare those really, since they serve completely different purpose. mutableState and others are relying on standard kotlin constructs under the hood. It just adds the "observable" part for compose kotlin compiler plugin to watch for.

CodePudding user response:

In the end I wrote a simple class to test the performances:

import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf


fun main() {
    val numbers = 1000000
    for (i in 1 until 11) {
        println("test number $i")

        testNormalVariable(numbers)
        testStateVariable(numbers)
        testNormalList(numbers)
        testStateList(numbers)

        println()
    }
}

fun testNormalVariable(numbers: Int) {
    var normalVariable = 0
    val startTime = System.currentTimeMillis()
    for (i in 0 until numbers) {
        normalVariable = i
    }
    val time = System.currentTimeMillis() - startTime
    println("testNormalVariable took $time ms")
}

fun testStateVariable(numbers: Int) {
    val stateVariable = mutableStateOf(0)
    val startTime = System.currentTimeMillis()
    for (i in 0 until numbers) {
        stateVariable.value = i
    }
    val time = System.currentTimeMillis() - startTime
    println("testStateVariable took $time ms")
}

fun testNormalList(numbers: Int) {
    val normalList = mutableListOf<Int>()
    val startTime = System.currentTimeMillis()
    for (i in 0 until numbers) {
        normalList.add(i)
    }
    val time = System.currentTimeMillis() - startTime
    println("testNormalList took $time ms")
}

fun testStateList(numbers: Int) {
    val stateList = mutableStateListOf<Int>()
    val startTime = System.currentTimeMillis()
    for (i in 0 until numbers) {
        stateList.add(i)
    }
    val time = System.currentTimeMillis() - startTime
    println("testStateList took $time ms")
}

And it became clear that mutableState and mutableStateList are significantly slower than a normal variable or a mutableList:

test number 1
testNormalVariable took 5 ms
testStateVariable took 82 ms
testNormalList took 48 ms
testStateList took 366 ms

test number 2
testNormalVariable took 5 ms
testStateVariable took 94 ms
testNormalList took 121 ms
testStateList took 368 ms

test number 3
testNormalVariable took 0 ms
testStateVariable took 35 ms
testNormalList took 30 ms
testStateList took 319 ms

test number 4
testNormalVariable took 0 ms
testStateVariable took 27 ms
testNormalList took 25 ms
testStateList took 299 ms

test number 5
testNormalVariable took 0 ms
testStateVariable took 32 ms
testNormalList took 29 ms
testStateList took 289 ms

test number 6
testNormalVariable took 0 ms
testStateVariable took 26 ms
testNormalList took 20 ms
testStateList took 310 ms

test number 7
testNormalVariable took 0 ms
testStateVariable took 41 ms
testNormalList took 30 ms
testStateList took 413 ms

test number 8
testNormalVariable took 0 ms
testStateVariable took 25 ms
testNormalList took 28 ms
testStateList took 142 ms

test number 9
testNormalVariable took 0 ms
testStateVariable took 93 ms
testNormalList took 29 ms
testStateList took 298 ms

test number 10
testNormalVariable took 0 ms
testStateVariable took 46 ms
testNormalList took 32 ms
testStateList took 309 ms
  • Related