I’ve started working with FastAPI to build Python-based backends. Both by name and by reputation, I hear FastAPI is pretty… fast. I also like a lot of the syntax for creating API’s with FastAPI versus Flask, but I was curious if I could get down to a solid understanding of just how much faster FastAPI is against Flask, or if it was all-talk.
I decided to create two identical API’s in both frameworks and do some scaled benchmarking to see which was faster. I would use exactly the same test script and keep everything very simple, just one GET endpoint and one POST endpoint. The endpoints would take a parameter (or JSON post data) as input with a delay value, the backend would sleep for that inputted amount of time and then would return. The tester will randomize delay inputs and time how long it takes for each endpoint to return, discounting the actual inputted delay.
My purpose for using a delay like this is to simulate something synchronous on the backend so there’s nothing sneaky that could make one framework appear faster than the other, btw.
So, the results: Fast seems to be faster.
I say seems to be because, frankly, this test is super simplistic. It’s single-threaded, running on localhost, only simulating actual computation instead of actually doing real computation and was done at a fairly low scale with tests consisting of 100 requests per each API framework.
That said, every time I ran the test, the winner was Fast, and the delta between the two was anywhere from 13% to 35%. That’s not nothing. Of the 10–15 tests I ran, Flask didn’t beat out Fast once. As for the issues with the testing methodology, from what I’ve seen, Fast excels at multi-threading and handling requests at scale compared to Flask. The style of simulating computation probably wouldn’t affect the test that much, and any change between running on localhost and running remotely would probably be pretty much equal between the two frameworks.
I’m going to do some more testing on this, but right now the benchmarking pretty much confirms what I’ve seen, that FastAPI is genuinely faster than Flask.