]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
📝 Update docs for `ORJSONResponse` with details about improving performance (#2615)
authorBen Falk <falk.ben@gmail.com>
Thu, 1 Sep 2022 09:59:05 +0000 (05:59 -0400)
committerGitHub <noreply@github.com>
Thu, 1 Sep 2022 09:59:05 +0000 (09:59 +0000)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
docs/en/docs/advanced/custom-response.md
docs_src/custom_response/tutorial001b.py

index 3ca47bf3b8a5c227ae065ec03fddd9aca7c6c8e9..ce2619e8de5fcc8f8b6d451c67a125f643c14846 100644 (file)
@@ -21,6 +21,12 @@ For example, if you are squeezing performance, you can install and use <a href="
 
 Import the `Response` class (sub-class) you want to use and declare it in the *path operation decorator*.
 
+For large responses, returning a `Response` directly is much faster than returning a dictionary.
+
+This is because by default, FastAPI will inspect every item inside and make sure it is serializable with JSON, using the same [JSON Compatible Encoder](../tutorial/encoder.md){.internal-link target=_blank} explained in the tutorial. This is what allows you to return **arbitrary objects**, for example database models.
+
+But if you are certain that the content that you are returning is **serializable with JSON**, you can pass it directly to the response class and avoid the extra overhead that FastAPI would have by passing your return content through the `jsonable_encoder` before passing it to the response class.
+
 ```Python hl_lines="2  7"
 {!../../../docs_src/custom_response/tutorial001b.py!}
 ```
index e98460372e3cfcb598935ec97d3fd6cc36b7bb55..95e6ca76379392109cc85378700a314efd86f439 100644 (file)
@@ -6,4 +6,4 @@ app = FastAPI()
 
 @app.get("/items/", response_class=ORJSONResponse)
 async def read_items():
-    return [{"item_id": "Foo"}]
+    return ORJSONResponse([{"item_id": "Foo"}])