https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

Ran Magen

10/01/2019, 10:42 PM
Hey, quick question, I was wondering about the cost of creating a
coroutineScope
that ends up doing nothing. For example should I bother writing:
Copy code
if (collection.isNotEmpty()) {
  coroutineScope {
    collection.forEach { item -> launch { doSomeComplexSuspendingWork(item) } }
  }
}
Or does it not really matter much and this would be just as efficient (and cleaner to read)
Copy code
coroutineScope {
  collection.forEach { item -> launch { doSomeComplexSuspendingWork(item) } }
}
o

octylFractal

10/01/2019, 10:57 PM
probably pre mature optimization, it could be an issue, but unlikely for most cases
l

louiscad

10/01/2019, 11:10 PM
Depends on if that code path is executed frequently or not. If it's once per app installation, don't bother. If it's quite often, the optimization might add up.
Or subtract down I should say.
2 Views