I collect all possible method and benchmark it using benchmark_harness package.
According to the result the recommended method is:
final List<int> c = a + b;
final List<int> c = [...a, ...b];
final List<int> c = [a, b].flattened;
Here is the benchmark code:
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:collection/collection.dart';
List<int> a = [1, 2, 3];
List<int> b = [4, 5, 6];
class Benchmark1 extends BenchmarkBase {
const Benchmark1() : super('c = a + b ');
@override
void run() {
final List<int> c = a + b;
}
}
class Benchmark2 extends BenchmarkBase {
const Benchmark2() : super('c = a.followedBy(b).toList() ');
@override
void run() {
final List<int> c = a.followedBy(b).toList();
}
}
class Benchmark3 extends BenchmarkBase {
const Benchmark3() : super('c = [a, b].expand((x) => x).toList() ');
@override
void run() {
final List<int> c = [a, b].expand((x) => x).toList();
}
}
class Benchmark4 extends BenchmarkBase {
const Benchmark4() : super('c = [a, b].reduce((value, element) => value + element) ');
@override
void run() {
final List<int> c = [a, b].reduce((value, element) => value + element);
}
}
class Benchmark5 extends BenchmarkBase {
const Benchmark5() : super('c = [a, b].fold([], (previousValue, element) => previousValue + element) ');
@override
void run() {
final List<int> c = [a, b].fold([], (previousValue, element) => previousValue + element);
}
}
class Benchmark6 extends BenchmarkBase {
const Benchmark6() : super('a.addAll(b) ');
@override
void run() {
a.addAll(b);
}
}
class Benchmark7 extends BenchmarkBase {
const Benchmark7() : super('c = <int>[...a, ...b] ');
@override
void run() {
final List<int> c = <int>[...a, ...b];
}
}
class Benchmark8 extends BenchmarkBase {
const Benchmark8() : super('c = List.from(a)..addAll(b) ');
@override
void run() {
final List<int> c = List.from(a)..addAll(b);
}
}
class Benchmark9 extends BenchmarkBase {
const Benchmark9() : super('c = concatLists([a, b]) ');
@override
void run() {
final List<int> c = concatLists([a, b]);
}
List<T> concatLists<T>(List<List<T>> listOfLists, [bool isGrowable = true]) {
if (listOfLists.first.isEmpty) {
throw Exception('first list of lists cannot be empty');
}
int totalLength = 0;
for (final list in listOfLists) {
totalLength += list.length;
}
List<T> result =
List<T>.filled(totalLength, listOfLists[0][0], growable: isGrowable);
int idx = 0;
for (final list in listOfLists) {
for (final item in list) {
result[idx] = item;
idx += 1;
}
// result.addAll(list); // at least 2x longer
}
return result;
}
}
class Benchmark10 extends BenchmarkBase {
const Benchmark10() : super('a.forEach((e) => b.add(e)) ');
@override
void run() {
a.forEach((e) => b.add(e));
}
}
class Benchmark11 extends BenchmarkBase {
const Benchmark11() : super('[a, b].flattened ');
@override
void run() {
[a, b].flattened;
}
}
class Benchmark12 extends BenchmarkBase {
const Benchmark12() : super('c = [ for() a[i], for() b[i] ] ');
@override
void run() {
final List<int> c = [
for (var i =0; i<a.length; i++) a[i],
for (var i =0; i<b.length; i++) b[i],
];
}
}
void main() {
// Benchmark1().report();
// Benchmark2().report();
// Benchmark3().report();
// Benchmark4().report();
// Benchmark5().report();
// Benchmark6().report();
// Benchmark7().report();
// Benchmark8().report();
// Benchmark9().report();
// Benchmark10().report();
// Benchmark11().report();
Benchmark12().report();
}
And the result:
c = a + b (RunTime): 0.5087239553641948 us.
c = a.followedBy(b).toList() (RunTime): 1.0383009808495096 us.
c = [a, b].expand((x) => x).toList() (RunTime): 1.9536091666666666 us.
c = [a, b].reduce((value, element) => value + element) (RunTime): 0.768018 us.
c = [a, b].fold([], (previousValue, element) => previousValue + element) (RunTime): 1.2125958937020531 us.
a.addAll(b) (RunTime): 1.7318055 us.
c = <int>[...a, ...b] (RunTime): 0.49604125553047174 us.
c = List.from(a)..addAll(b) (RunTime): 0.60551975 us.
c = concatLists([a, b]) (RunTime): 0.8764710383981061 us.
a.forEach((e) => b.add(e)) (RunTime): 1.4383115 us.
[a, b].flattened (RunTime): 0.471882834053767 us.
c = [ for() a[i], for() b[i] ] (RunTime): 0.5955005 us.
Dart Version:
Dart SDK version: 3.4.3 (stable) (Tue Jun 4 19:51:39 2024 +0000) on "macos_x64"