FastAPI vs. Express.js vs. Flask vs. Nest.js Benchmark _ by Travis Luong _ Level Up Coding
FastAPI vs. Express.js vs. Flask vs. Nest.js Benchmark _ by Travis Luong _ Level Up Coding
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
Travis Luong
Jan 1 · 6 min read · Listen
FastAPI + psycopg2
FastAPI + SQLModel
Express.js + pg
Nest.js + Prisma
$ uvicorn fast_psycopg:app
$ wrk http://localhost:8000
$ uvicorn fast_sqlmodel:app
$ wrk http://localhost:8000
Express.js + pg
$ node express_pg.js
$ wrk http://localhost:3000
Nest.js + Prisma
$ npm start
$ wrk http://localhost:3000
Conclusion
It looks like the minimalist Express.js + pg combo wins this benchmarking
round, followed by Flask with 4 gunicorn workers and Nest.js + Prisma.
Flask with the “flask run” server had a large number of non-2xx or 3xx
responses, as expected of a development server.
Is it safe to say that FastAPI is not on par with the Node.js frameworks in terms of
performance? Or have I conducted the tests incorrectly? Perhaps I’m not
leveraging FastAPI’s async functionality in the right way?
At the end of the day, it probably doesn’t matter too much which framework you
choose. Just use whatever language you’re most productive in since developer
time is usually more expensive than computing power.
Update 1/3/22
Thanks to Dmitry for pointing out that I should use the asyncpg library instead of
psycopg2.
It appears that FastAPI is still behind Node.js in performance despite adding the
async database drivers. The JSON serialization is a possible bottleneck. If anyone
knows how to optimize this, please let me know!
$ uvicorn fast_asyncpg:app
$ wrk http://localhost:8000
Search
$ uvicorn fast_databases:app
$ wrk http://localhost:8000
Travis Luong
Running 10s test @ http://localhost:8000 120 Followers
2 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev Programmer
Latency 37.31ms 4.39ms 95.69ms 94.49%
Req/Sec 134.30 12.86 151.00 77.50%
Follow
2697 requests in 10.08s, 20.63MB read
Requests/sec: 267.69
Transfer/sec: 2.05MB
More from Medium
Auns… in JavaSc…
Top 5 Node.js
Real-time
Here are the new results! Libraries Ruling …
Andrew Reddikh
Meriç… in Better…
$ uvicorn fast_psycopg:app
$ wrk http://localhost:8000/orjson Migrating a
PostgreSQL
Database From…
Running 10s test @ http://localhost:8000/orjson
2 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 12.06ms 1.77ms 30.71ms 87.39%
Req/Sec 415.52 23.51 464.00 72.50%
8317 requests in 10.05s, 63.61MB read
Help Status Writers Blog Careers
Requests/sec: 827.30 279 11 Privacy Terms About Knowable
Transfer/sec: 6.33MB
$ uvicorn fast_asyncpg:app
$ wrk http://localhost:8000/orjson
$ uvicorn fast_asyncpg:app
$ wrk http://localhost:8000/ujson
Final Conclusion
The winner is FastAPI + asyncpg + 4 gunicorn workers + ujson.
FastAPI is definitely fast, on par with Node.js, and lives up to the hype! Well,
according to these benchmarks.
Just make sure you’re using the right libraries with it!
I realized there is another flaw in the benchmark. Node.js has a cluster mode,
which I was unaware of. For new benchmarks and a complete ranking, check out
part 2 of this benchmarking article:
https://medium.com/@travisluong/fastapi-vs-fastify-vs-spring-boot-vs-gin-
benchmark-b672a5c39d6c
If you’re interested in learning more about FastAPI and other amazing tools,
check out my Full Stack Tutorial:
https://medium.com/@travisluong/full-stack-next-js-fastapi-postgresql-
tutorial-86f0af0747b7
A monthly summary of the best stories shared in Level Up Coding Take a look.