Open dtonhofer opened 8 months ago
@eernstg : Could use your input here. Thanks!
The reader basically just needs to know that elementAt
can be expensive, and if we want better performance it might help to do .toList()
and use operator []
.
In that situation it won't help much to know that "elementAt
might be fast", it really only helps if it makes the reader think "do .toList()
and use operator []
" ("do .toList()
and use elementAt
" incurs an extra call so it does actually make sense to say "use operator []
").
So perhaps the text should just say that? Something like "Note that elementAt
can have complexity which is linear in the size of the iterable (which is very expensive in some cases). If you plan to do it repeatedly then consider doing .toList()
on the iterable and use operator []
."
Page URL
https://dart.dev/codelabs/iterables/
Page source
https://github.com/dart-lang/site-www/tree/main/./src/content/codelabs/iterables.md
Describe the problem
We read in What is an Iterable?
That would depend on the implementation of the interface.
Expected fix
Suggesting to add "...if the underlying data structure does not allow for a more efficient method", as would be expected.
Because clearly, using
elementAt()
on aList
does not result in linear traversal, but proper indexed access.The description for elementAt says:
We can also run a quick test of efficiency regarding accessing a
List
withelementAt()
vs accessing aSet
withelementAt()
:Result:
Additional context
No response
I would like to fix this problem.