Skip to content

feat: allow different json library imports #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
May 29, 2025
Merged

feat: allow different json library imports #285

merged 17 commits into from
May 29, 2025

Conversation

EvanR-Dev
Copy link
Contributor

@EvanR-Dev EvanR-Dev commented May 19, 2025

Allow users to import orjson in their requirements.txt - the library will use that over the standard json import if installed. Here is a sample measure of performance differences. The following code calculates the average time for how long it takes to deserialize and serialize a Cosmos document JSON (~5 KB), over 10,000 runs

Note: measured in microseconds, not milliseconds

@app.route(route="items", auth_level=func.AuthLevel.ANONYMOUS)
@app.cosmos_db_input(arg_name="inputDocument", 
                     database_name="test",
                     container_name="items",
                     id="test-json",
                     connection="pythonworker37cdb_DOCUMENTDB")
def test_function(req: func.HttpRequest, inputDocument: func.DocumentList) -> str:
    if not inputDocument:
        return "Not found"
    
    runs = 10_000
    start = time.perf_counter()
    for _ in range(runs):
        json_string = func.Document.to_json(inputDocument[0])
        _ = func.Document.from_json(json_string)

    end = time.perf_counter()
    avg_ms = ((end - start) / runs) * 1_000_000

    logging.info(f"Time taken: {avg_ms:.2f}ms")

    return "Item fetched successfully"

import json
image

import orjson
image

(Not being added, but just for comparison):
import ujson
image

import simplejson
image

@EvanR-Dev EvanR-Dev self-assigned this May 19, 2025
@EvanR-Dev EvanR-Dev marked this pull request as ready for review May 20, 2025 15:50
@gavin-aguiar
Copy link
Contributor

Update the description and keep only ORJSON

@hallvictoria hallvictoria merged commit 498d0c9 into dev May 29, 2025
16 checks passed
@hallvictoria hallvictoria deleted the evanroman/json branch May 29, 2025 15:39
@EvanR-Dev
Copy link
Contributor Author

EvanR-Dev commented Jun 2, 2025

Orjson is especially effective when the JSON payload increases in size. The improvements range from 15% (~1 KB payload) to even above 100% as the payload size increases

In these scenarios we have an EventHub trigger with an EventHub output binding that receives HTTP requests and sends it to an EventHub after deserializing the payload

Orjson:
image

Std json:
image
(some were not able to respond back in time as well)

If we purposely deserialize and serialize 10,000 times using the Cosmos library and measure the avg time (again)

Orjson:
image

Std json:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants