Flutter Benchmark Tuesday: Dio vs http — Which Network Library is Faster?

Alex Josef Bigler
Full Struggle Developer
5 min readJul 25, 2023

Hello, Flutter stranger! It’s another exciting Tuesday, and you know what that means — it’s time for another round of Flutter made of duct tape and bubble gum benchmarks. Today, we’re going to dive into the world of network libraries. We’ll be pitting Dio against http in a series of network requests to see which one comes out on top in terms of speed. So, let’s get started!

C’mon, let’s go

Just like in our previous tests, we’re going to keep things simple and straightforward. We’ll be making a series of GET requests to my very own fake API, which I painstakingly created for such occasions.

If you’re curious about how I built this API, you can read all about it in my previous article (check here 👈). We’ll be using the same endpoint for both libraries to ensure a fair comparison.

Here’s how we set up our test:

import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:dio/dio.dart';
import 'package:http/http.dart' as http;

void main() {
Dio dio = Dio();
var client = http.Client();
var url = 'https://fake-api.sandbox.koodalabs.com/posts/1';

test('benchmark Dio and http', () async {
String? result; // So that the loop isn't optimized out

// Benchmark Dio
var dioStopwatch = Stopwatch()..start();
for (int i = 0; i < 100; i++) {
var response = await dio.get(url);
result = jsonEncode(response.data);
}
dioStopwatch.stop();
print('Dio get request took ${dioStopwatch.elapsedMilliseconds} millisecond.');

// Benchmark http
var httpStopwatch = Stopwatch()..start();
for (int i = 0; i < 100; i++) {
var response = await client.get(Uri.parse(url));
result = response.body;
}
httpStopwatch.stop();
print('http get request took ${httpStopwatch.elapsedMilliseconds} millisecond.');
});
}

Lines 14–19. These lines create a Stopwatch, start it, make 100 GET requests using the Dio client, stop the Stopwatch, and print the elapsed time. The await keyword is used to wait for the Dio client's get method to complete before continuing.

Lines 21–26. These lines do the same as lines 14–19, but with the http client instead of the Dio client.

In this high-octane race of HTTP clients, every millisecond counts. It’s a battle of speed, efficiency, and raw power. Who will come out on top? Only the Stopwatch knows for sure.

TL;DR

Ah, the trusty Stopwatch, the unsung hero of our little race. You see, in the world of programming, especially when we’re dealing with asynchronous operations like HTTP requests, time is a slippery concept. It’s not as simple as just looking at the clock on the wall.

When we send off a GET request into the internet, we don’t know exactly when it’s going to come back. It might take a leisurely stroll through a few servers, stop for a coffee at a router, and then eventually mosey its way back to us with the data we asked for. Or it might sprint straight there and back. The point is, we don’t know.

That’s where our friend Stopwatch comes in. It starts ticking the moment we send off our request, and it doesn’t stop until the data is safely back in our hands. It’s like a faithful hound that follows our request on its journey, keeping track of exactly how long it takes.

After running the test, we get the following results:

And the winner is… http! With a time of approximately 14.40 seconds, http is the fastest network library among the two. Dio comes in second place with a time of approximately 15.12 seconds.

That’s about it

Well, well, well, who would have thought? Certainly not me, who has been writing all my projects using bare http, without any doubt in my mind about its performance. It’s almost as if I knew the outcome of this benchmark beforehand. But, of course, that’s just a coincidence, right?

That’s it for this week’s Flutter Benchmark Tuesday. I hope you found this comparison helpful. As always, I encourage you to run your own tests and share your results. Remember, the goal is not to find the “best” library, but to understand how different tools perform under different conditions.

Until next time, keep coding and keep benchmarking!

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.