FastAPI is recognized as one of the fastest Python frameworks available in independent TechEmpower benchmarks. However, when evaluating performance, it is important to understand the architectural hierarchy and the trade-offs between different types of tools:
The Performance Hierarchy
- Uvicorn (ASGI Server): The base layer. It has the highest performance because it lacks application-level logic. You do not write applications directly in Uvicorn; you use it to run Starlette or FastAPI.
- Starlette (Web Microframework): Built on top of Uvicorn. It provides routing and web tools. It is slightly slower than Uvicorn due to the additional code execution required for framework features.
- FastAPI (API Microframework): Built on top of Starlette. It adds essential API features like data validation, serialization, and automatic documentation.
How to Compare Tools Correctly
To get a meaningful comparison, compare tools within the same category:
- Compare ASGI Servers (like Uvicorn) against other servers like Daphne, Hypercorn, or uWSGI.
- Compare Web Frameworks (like Starlette) against other frameworks like Sanic, Flask, or Django.
- Compare API Frameworks (like FastAPI) against other tools that provide integrated data validation, serialization, and documentation, such as NestJS, Molten, or Flask-apispec.
The FastAPI Performance Advantage
While FastAPI has more features than Starlette, using it does not necessarily result in a performance penalty compared to building your own solution. If you use Starlette directly to build an API, you must manually implement data validation and serialization. Since these tasks are often the most computationally expensive parts of an application, a custom implementation would likely result in similar overhead to FastAPI, while costing more development time and increasing the risk of bugs.