Serve your data blazingly fast with Sqlite3 and Javascript
Tags: sqlite, javascript, rust, webperf
Published: 2022-10-16 18:03:05

# Serve your data blazingly fast with Sqlite3 and Javascript

I love SQLite3. It is my de-facto database for personal projects. I have seen people import [millions of rows per minute](https://blog.metaobject.com/2021/07/inserting-130m-sqlite-rows-per.html). I was stunned to see it used as an [NOSQL Database](https://antonz.org/json-virtual-columns/). I adore that you can host SQLite databases [everywhere](https://phiresky.github.io/blog/2021/hosting-sqlite-databases-on-github-pages/). SQLite3 versatilety is amazing.

I wanted to explore the performance of SQLite3 when it is used with a web server. Indeed with 2022 Europe energy crisis It is vital to find cost efficient solutions.

I experimented with various programming languages: Go, Ruby, JavaScript, Rust. 

**My goal: get a feel for what is achievable concurrency wise.** 

I balanced speed with code complexity. I did not want to spend too much time on each implementation. The SQLite queries I used are simple. One insert statement, one query statement. This is not a test to verify SQLite performance. I know SQLite is fast. This is a test to see the blocking impact of SQLite queries on web servers.

The JavaScript results amazed me.

**node**:
- With an *sqlite insert*: 16k requests per second
- With an *sqlite select*: 31k requests per second

![node-sqlite3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c67jh1k9zeuhfc19oq7r.png)

**rust**:
- With an *sqlite insert*: 14k requests per second
- With an *sqlite select*: 61k requests per second

*rust* is slower with inserts but twice as fast with queries.

![rust sqlite3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sa4wn8rrqssnb0z3l3ti.png)

**bun.sh** (a new javascript runtime):
- With an *sqlite insert*: 21k requests per second
- With an *sqlite select*: 75k requests per second

![bun.sh sqlite3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9u7sepajz9ez7mipgz2e.png)

What? *bun.sh* is twice as fast on inserts and better on selects.
We live in a time when anyone can serve tens of thousands of requests per second from one server (the one I used is 3 years old).
You don't need a fancy database.
An sqlite3 database hosted on the same host is more than enough.

I remember hearing [*John Carmack* say](https://www.youtube.com/watch?v=rczu8kc8JZA&t=35s)

	The engineering under JavaScript is really pretty phenomenal

*bun.sh* is a new example of phenomenal engineering for JavaScript.
When you combine all the blazingly fast JavaScript optimisations with Sqlite3 you get performance that is on par with compiled languages.

I imagine I could get *rust* to be faster.
But that's going to require more work to figure out the implementation details.
*bun.sh* + *SQLite3* are more than enough for all my performance needs.

## References

- *John Carmack* on Javascript: https://www.youtube.com/watch?v=rczu8kc8JZA
- [bun.sh](https://bun.sh/)
- blazingly fast like [ThePrimeagen](https://www.twitch.tv/theprimeagen)
- My test code: https://github.com/aurelienbottazini/sqlite-performance-test
- [Insert millions of rows per minute](https://blog.metaobject.com/2021/07/inserting-130m-sqlite-rows-per.html)
- [SQLite as a NOSQL Database](https://antonz.org/json-virtual-columns/)
- [Host SQLite on Github pages](https://phiresky.github.io/blog/2021/hosting-sqlite-databases-on-github-pages/)