Flutter Benchmark Tuesday: SQLite vs. Hive — how popular articles deceive us

Alex Josef Bigler
Full Struggle Developer
6 min readMay 2, 2023

Hello everyone. This is another story of our regular segment “testing obvious things on Tuesdays”. And this time, the topic of choosing a database for local data storage in a Flutter application caught my attention. This topic is very overused, and there are many articles written about it, most of which only touch on the practical comparison of three alternative data storage options: Hive, SQLite, and SharedPreferences.

I won’t waste your time describing each of them since if you’re reading this article, you probably already know what they are. And I will only compare Hive and SQLite because I cannot consider SharedPreferences a database (you can throw lemons at me, I already poured myself some tequila 🤙).

So, what does an average article about the choice of database look like?

Hello. Use Hive, don’t use SQLite. Goodbye.

And to justify this, such an interesting infographic is presented:

Hive is in Outer Space. This is a screenshot from the official documentation, you can check it yourself

Actually, it looks sad because anyone who has even seen how a real adult backend is built will not compare MongoDB and PostgreSQL only by performance. Seriously, why mislead naive beginners who want to make mobile apps?

Sure, let’s hold off on conclusions until the end of the article. You’ll see everything for yourself.

To start, let’s create a file called my_class.dart, where we will describe a test object for reading/writing from the database. After all, we want to test real usage, not just integers and strings like in the screenshots above, right?

import 'package:hive/hive.dart';

class MyClass {
int id;
String name;

MyClass(this.id, this.name);
}

class MyClassAdapter extends TypeAdapter<MyClass> {
@override
final typeId = 0;

@override
MyClass read(BinaryReader reader) {
final id = reader.readInt();
final name = reader.readString();
return MyClass(id, name);
}

@override
void write(BinaryWriter writer, MyClass obj) {
writer.writeInt(obj.id);
writer.writeString(obj.name);
}
}

Next, as usual, let’s create a _test file:

import 'package:flutter/widgets.dart';
import 'package:benchmarking/benchmarking.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:sqflite/sqflite.dart';
import 'dart:math';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

import 'my_class.dart';

void main() async {
// Initialize Hive
WidgetsFlutterBinding.ensureInitialized();
Hive.init('.');

// Register adapter if necessary
Hive.registerAdapter(MyClassAdapter());

// Generate test data
final testData = List.generate(1000, (index) => MyClass(index, 'test'));

// Benchmark SQLite
databaseFactory = databaseFactoryFfi;
final db = await openDatabase('test.db', version: 1, onCreate: (Database db, int version) async {
await db.execute('CREATE TABLE Test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)');
});
syncBenchmark('SQLite Insert', () async {
for (int i = 0; i < testData.length; i++) {
await db.insert('Test', {'id': testData[i].id, 'name': testData[i].name});
}
}).report();

syncBenchmark('SQLite Select', () async {
await db.rawQuery('SELECT * FROM Test');
}).report();

// Benchmark Hive
syncBenchmark('Hive Insert', () async {
final box = await Hive.openBox<MyClass>('benchmark');
for (int i = 0; i < testData.length; i++) {
await box.put(i, testData[i]);
}
}).report();

syncBenchmark('Hive Select', () async {
final box = await Hive.openBox<MyClass>('benchmark');
box.values.toList();
}).report();

// Clean up
db.close();
Hive.deleteBoxFromDisk('benchmark');
}

In our experiment, we created 1000 objects of the MyClass class with random values, and then performed data insertion and selection in SQLite and Hive.

So, lets see the result:

To make it easier for you to see, I’ll translate the results into a graph format:

Okay, I have one question.

Seriously

Although Hive showed better performance in this benchmark, the performance difference between Hive and SQLite is not significant enough to write an article about Hive in a religious frenzy.

The main advantage of Hive is its ease of use. Hive allows for quickly creating and using objects. This can be especially useful for applications that do not require a large amount of data and require fast response to user input.

On the other hand, SQLite is a more powerful tool for data management, especially for large-scale projects with a large amount of data. SQLite can provide many options for data management, such as integrity constraints, triggers, etc.

Thus, when choosing between Hive and SQLite, it is necessary to consider the application requirements and the volume of data that needs to be processed. If a quick and simple solution is needed for small amounts of data, then Hive is the better choice. If the project is large-scale, requires more advanced data management, and involves large amounts of data, then SQLite is the better choice.

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.