Flutter Benchmark Tuesday: Apologies to Zoomers

Alex Josef Bigler
Full Struggle Developer
7 min readMay 9, 2023

If you are reading this article, you have probably already seen my previous article (check here šŸ‘ˆ) in which I compared the map method and the for loop and unexpectedly won the map method in the benchmark. Despite the fact that the article began with my statement that the map method can not be faster than the for loop, and despite the ironic style of the article as a whole, it went viral. And in order to clarify everything, I am writing this text.

To start, let me tell you a short story from my high school:

A team of truck drivers from city A managed to deliver 100,000 packs of CD discs from city A to city B overnight. They thus achieved a data transfer rate of 2.43 terabytes per second over a distance of more than 500 km without using expensive cables.

And while you are googling what CD disks are, I will immediately write the code for an extended fair comparison of iteration methods.

Well

I will add several significant changes to the previous test:

  1. Instead of simply assigning a value to the result variable, we will add elements to the results list. Then, before each call to report(), we will clear the results list to ensure that each test starts from a clean state. This will convince the compiler that the operations are indeed needed and should not be optimized.
  2. We will add other loops to the comparison: forEach, for-in, and while. Just to demonstrate the fundamental differences between them.
import 'dart:convert';
import 'package:benchmarking/benchmarking.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('benchmark forEach, for, for-in, and while with iterator', () {
List<String> results = [];

String? result;

var json =
"{\"latitude\":52.52,\"longitude\":13.419998,\"generationtime_ms\":0.1590251922607422,\"utc_offset_seconds\":0,\"timezone\":\"GMT\",\"timezone_abbreviation\":\"GMT\",\"elevation\":38.0,\"current_weather\":{\"temperature\":13.3,\"windspeed\":15.6,\"winddirection\":40.0,\"weathercode\":3,\"is_day\":1,\"time\":\"2023-04-18T14:00\"},\"hourly_units\":{\"time\":\"iso8601\",\"temperature_2m\":\"Ā°C\"},\"hourly\":{\"time\":[\"2023-04-18T00:00\",\"2023-04-18T01:00\",\"2023-04-18T02:00\",\"2023-04-18T03:00\",\"2023-04-18T04:00\",\"2023-04-18T05:00\",\"2023-04-18T06:00\",\"2023-04-18T07:00\",\"2023-04-18T08:00\",\"2023-04-18T09:00\",\"2023-04-18T10:00\",\"2023-04-18T11:00\",\"2023-04-18T12:00\",\"2023-04-18T13:00\",\"2023-04-18T14:00\",\"2023-04-18T15:00\",\"2023-04-18T16:00\",\"2023-04-18T17:00\",\"2023-04-18T18:00\",\"2023-04-18T19:00\",\"2023-04-18T20:00\",\"2023-04-18T21:00\",\"2023-04-18T22:00\",\"2023-04-18T23:00\",\"2023-04-19T00:00\",\"2023-04-19T01:00\",\"2023-04-19T02:00\",\"2023-04-19T03:00\",\"2023-04-19T04:00\",\"2023-04-19T05:00\",\"2023-04-19T06:00\",\"2023-04-19T07:00\",\"2023-04-19T08:00\",\"2023-04-19T09:00\",\"2023-04-19T10:00\",\"2023-04-19T11:00\",\"2023-04-19T12:00\",\"2023-04-19T13:00\",\"2023-04-19T14:00\",\"2023-04-19T15:00\",\"2023-04-19T16:00\",\"2023-04-19T17:00\",\"2023-04-19T18:00\",\"2023-04-19T19:00\",\"2023-04-19T20:00\",\"2023-04-19T21:00\",\"2023-04-19T22:00\",\"2023-04-19T23:00\",\"2023-04-20T00:00\",\"2023-04-20T01:00\",\"2023-04-20T02:00\",\"2023-04-20T03:00\",\"2023-04-20T04:00\",\"2023-04-20T05:00\",\"2023-04-20T06:00\",\"2023-04-20T07:00\",\"2023-04-20T08:00\",\"2023-04-20T09:00\",\"2023-04-20T10:00\",\"2023-04-20T11:00\",\"2023-04-20T12:00\",\"2023-04-20T13:00\",\"2023-04-20T14:00\",\"2023-04-20T15:00\",\"2023-04-20T16:00\",\"2023-04-20T17:00\",\"2023-04-20T18:00\",\"2023-04-20T19:00\",\"2023-04-20T20:00\",\"2023-04-20T21:00\",\"2023-04-20T22:00\",\"2023-04-20T23:00\",\"2023-04-21T00:00\",\"2023-04-21T01:00\",\"2023-04-21T02:00\",\"2023-04-21T03:00\",\"2023-04-21T04:00\",\"2023-04-21T05:00\",\"2023-04-21T06:00\",\"2023-04-21T07:00\",\"2023-04-21T08:00\",\"2023-04-21T09:00\",\"2023-04-21T10:00\",\"2023-04-21T11:00\",\"2023-04-21T12:00\",\"2023-04-21T13:00\",\"2023-04-21T14:00\",\"2023-04-21T15:00\",\"2023-04-21T16:00\",\"2023-04-21T17:00\",\"2023-04-21T18:00\",\"2023-04-21T19:00\",\"2023-04-21T20:00\",\"2023-04-21T21:00\",\"2023-04-21T22:00\",\"2023-04-21T23:00\",\"2023-04-22T00:00\",\"2023-04-22T01:00\",\"2023-04-22T02:00\",\"2023-04-22T03:00\",\"2023-04-22T04:00\",\"2023-04-22T05:00\",\"2023-04-22T06:00\",\"2023-04-22T07:00\",\"2023-04-22T08:00\",\"2023-04-22T09:00\",\"2023-04-22T10:00\",\"2023-04-22T11:00\",\"2023-04-22T12:00\",\"2023-04-22T13:00\",\"2023-04-22T14:00\",\"2023-04-22T15:00\",\"2023-04-22T16:00\",\"2023-04-22T17:00\",\"2023-04-22T18:00\",\"2023-04-22T19:00\",\"2023-04-22T20:00\",\"2023-04-22T21:00\",\"2023-04-22T22:00\",\"2023-04-22T23:00\",\"2023-04-23T00:00\",\"2023-04-23T01:00\",\"2023-04-23T02:00\",\"2023-04-23T03:00\",\"2023-04-23T04:00\",\"2023-04-23T05:00\",\"2023-04-23T06:00\",\"2023-04-23T07:00\",\"2023-04-23T08:00\",\"2023-04-23T09:00\",\"2023-04-23T10:00\",\"2023-04-23T11:00\",\"2023-04-23T12:00\",\"2023-04-23T13:00\",\"2023-04-23T14:00\",\"2023-04-23T15:00\",\"2023-04-23T16:00\",\"2023-04-23T17:00\",\"2023-04-23T18:00\",\"2023-04-23T19:00\",\"2023-04-23T20:00\",\"2023-04-23T21:00\",\"2023-04-23T22:00\",\"2023-04-23T23:00\",\"2023-04-24T00:00\",\"2023-04-24T01:00\",\"2023-04-24T02:00\",\"2023-04-24T03:00\",\"2023-04-24T04:00\",\"2023-04-24T05:00\",\"2023-04-24T06:00\",\"2023-04-24T07:00\",\"2023-04-24T08:00\",\"2023-04-24T09:00\",\"2023-04-24T10:00\",\"2023-04-24T11:00\",\"2023-04-24T12:00\",\"2023-04-24T13:00\",\"2023-04-24T14:00\",\"2023-04-24T15:00\",\"2023-04-24T16:00\",\"2023-04-24T17:00\",\"2023-04-24T18:00\",\"2023-04-24T19:00\",\"2023-04-24T20:00\",\"2023-04-24T21:00\",\"2023-04-24T22:00\",\"2023-04-24T23:00\"],\"temperature_2m\":[6.0,5.7,5.2,4.7,4.4,4.3,5.2,6.7,8.2,8.8,10.4,11.7,11.9,12.6,13.3,13.6,12.6,11.9,10.8,9.8,8.6,7.0,5.8,5.1,4.6,4.0,3.5,3.1,2.7,2.9,4.1,6.1,7.9,9.6,11.0,11.8,11.7,11.2,9.9,7.6,5.9,5.1,5.5,5.5,5.7,5.9,6.1,6.1,6.0,5.8,5.7,5.6,5.6,5.7,6.1,6.6,7.0,7.7,8.6,10.9,14.6,15.5,15.9,15.2,15.2,14.7,13.4,12.1,11.2,10.5,10.0,9.6,9.2,8.8,8.4,8.1,7.8,7.9,9.0,10.7,12.7,14.5,16.0,17.1,17.9,18.2,18.2,17.8,17.0,16.0,14.6,13.5,12.5,11.3,10.6,10.2,9.6,9.0,8.5,8.1,8.0,8.2,9.1,10.6,12.6,15.1,16.4,17.5,18.5,18.7,18.6,18.0,17.3,16.4,15.3,14.8,14.4,13.9,13.6,13.4,13.0,12.6,12.2,11.8,11.8,11.9,12.0,9.1,10.0,11.1,11.9,12.7,13.5,13.9,14.2,14.2,14.0,13.6,12.9,12.3,11.7,10.9,10.6,10.3,9.7,9.0,8.1,7.1,6.8,6.6,6.7,7.1,7.6,8.4,9.0,9.6,10.2,10.7,11.1,11.2,10.9,10.3,9.4,8.8,8.0,7.1,6.6,6.1]}}";

var decodedJson = jsonDecode(json);
var neededArray = decodedJson['hourly']['time'];

syncBenchmark('forEach over needed array', () {
neededArray.forEach((item) {
results.add(item);
});
results.clear();
}).report(units: neededArray.length);

syncBenchmark('For loop over needed array', () {
for (int i = 0; i < neededArray.length; i++) {
results.add(neededArray[i]);
}
results.clear();
}).report(units: neededArray.length);

syncBenchmark('For-in loop over needed array', () {
for (var item in neededArray) {
results.add(item);
}
results.clear();
}).report(units: neededArray.length);

syncBenchmark('While loop with iterator over needed array', () {
var iterator = neededArray.iterator;
while (iterator.moveNext()) {
results.add(iterator.current);
}
results.clear();
}).report(units: neededArray.length);

syncBenchmark('Map over needed array', () {
results = neededArray.map<String>((item) => item.toString()).toList();
results.clear();
}).report(units: neededArray.length);
});
}

So, letā€™s see the result:

Here is an explanation of the results from the perspective of how the different iteration methods work:

  1. forEach: This method applies the passed function to each element of the list. In this case, the performance of forEach is 168,000,000 elements per second. This is because forEach is based on functional programming and provides a fairly fast iteration over elements.
  2. For loop: The for loop is a classic way of iterating over the elements of a list using an index. In this case, the performance of the for loop is also 168,000,000 elements per second. This is because the for loop is a native language construct and is optimized for fast iteration.
  3. For-in loop: The for-in loop is a higher-level way of iterating over the elements of a list. In this test, the performance is 84,000,000 elements per second. This is because the for-in loop uses iterators under the hood and does not require an index to access elements, which can reduce performance compared to the for and forEach loops.
  4. While loop with iterator: In this case, we manually use an iterator with a while loop. The performance is 84,000,000 elements per second. This is because iterators provide lower-level control over the iteration process, which can reduce performance compared to other methods.
  5. Map: The map method creates a new collection by applying the passed function to each element of the source list. In this case, the performance of the map method is 56,000,000 elements per second. This is because the map method creates a new list, which leads to additional memory allocation and copying of elements.

And guys, always remember one simple thing:

map is not a loop

Yes, this method is often used when creating arrays, but it is not entirely correct to compare it with loops. Itā€™s like transporting CD disks by trucks ā€” itā€™s a more complex and expensive way of working with data.

Thatā€™s all

To avoid falling into the trap of clickbait articles, try to communicate in person with experienced developers. You will be very lucky if among your colleagues there are leads and seniors with good methodologies, and they share them with others. This is a foundation.

Subscribe so you donā€™t miss any new groundbreaking posts.

You might also be interested in my other benchmarks:

Investing stuff:

Tech stuff:

Or a whole series of articles about working with REST API (itā€™s a real shit like Santa Barbara):

--

--

Alex Josef Bigler
Full Struggle Developer

Enthusiast of new technologies, entrepreneur, and researcher. Writing about IT, economics, and other stuff. Exploring the world through the lens of data.