kotlinandroid-jetpack-composecompose-desktopjetbrains-compose

Jetpack Compose mutableStateList vs mutableList Performance


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.


Solution

  • 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