Which of these is likely to perform better? I assu...
# getting-started
g
Which of these is likely to perform better? I assume the
List
version but you never know, figured I'd ask others:
Copy code
fun recur(...): Sequence<*> = sequence {
    yield(...)
    getQueryRelations(innerRequest).forEach {
        yieldAll(
            recur(...)
        )
    }
}
Copy code
fun recur(...): List<*> {
    return listOf(...) + getQueryRelations(innerRequest).flatMap {
        recur(...)
    }
}
e
possibly irrelevant, but if
getQueryRelations(innerRequest)
is a
suspend fun
then your
sequence
version can't be used directly.
sequenceOf(...) + getQueryRelations(innerRequest).asSequence().flatMap { ... }
could be used though
I would tend to use
Copy code
buildList {
    add(...)
    getQueryRelations(innerRequest).flatMapTo(this) { recur(...) }
}
or
Copy code
getQueryRelations(innerRequest).flatMapTo(mutableListOf(...)) { recur(...) }
instead of your second option, to avoid one whole list copy
but the performance is likely to vary on several factors
g
.flatMapTo(this)
I had never thought of that, that's pretty clever!