8d3b6ce54c
It is faster.
[expand: 102]
[flattened: 131]
[flattened2: 37]
import 'package:collection/collection.dart';
main() {
const outerLength = 100;
const innerLength = 10;
final listOfLists = List.generate(outerLength, (i) {
return List.generate(innerLength, (j) => i * innerLength + j);
});
for (var i = 0; i < 10; i++) {
f(listOfLists);
}
}
void f(List<List<int>> listOfLists) {
const repeatCount = 10000;
{
final timer = Stopwatch()..start();
for (var i = 0; i < repeatCount; i++) {
listOfLists.expand((e) => e).toList();
}
print('[expand: ${timer.elapsedMilliseconds}]');
}
{
final timer = Stopwatch()..start();
for (var i = 0; i < repeatCount; i++) {
listOfLists.flattened.toList();
}
print('[flattened: ${timer.elapsedMilliseconds}]');
}
{
final timer = Stopwatch()..start();
for (var i = 0; i < repeatCount; i++) {
listOfLists.flattened2.toList();
}
print('[flattened2: ${timer.elapsedMilliseconds}]');
}
}
extension ListListExtensions<T> on List<List<T>> {
Iterable<T> get flattened2 {
return [
for (final elements in this) ...elements,
];
}
}
Change-Id: I0bf9dc0c8735fe62aab69cbce276254e2db110f9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/345202
Reviewed-by: Jacob Richman <jacobr@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>