Why orjson Beats ujson for Fast Python JSON Serialization

This article compares Python's built‑in json module with faster alternatives like ujson, rapidjson, and the Rust‑based orjson, showing benchmark results, installation steps, code examples, and a known ujson indentation bug to help developers choose the most efficient JSON library.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Why orjson Beats ujson for Fast Python JSON Serialization

When using json.dumps(data) in Python, the built‑in json library can be surprisingly slow even for modest data structures, prompting a search for faster JSON handling libraries. The author compared three libraries: ujson (UltraJSON, written in C), python‑rapidjson (C++), and the standard json module, later adding the Rust‑based orjson after discovering its impressive performance.

Installation commands are straightforward:

pip install python-rapidjson pip install simplejson

A benchmark script ( test.py) generates a large list of dictionaries and measures the time taken by each library's dumps() function. The script imports the chosen library based on a command‑line argument and prints the elapsed time.

from time import time
import sys, string
num = int(sys.argv[1])
lib = sys.argv[2]
items = []
for i in range(num):
    items.append({c: c for c in string.ascii_letters})
start = time()
if lib == 'ujson':
    import ujson
    ujson.dumps(items)
elif lib == 'rapidjson':
    import rapidjson
    rapidjson.dumps(items)
elif lib == 'orjson':
    import orjson
    orjson.dumps(items)
else:
    import json
    json.dumps(items)
print(time() - start)

Running the script with various data sizes (1 000 to 1 000 000 items) and libraries produced timing results that match benchmarks from other sources: orjson consistently outperformed ujson, which in turn was faster than the standard json module, while rapidjson showed comparable speed to ujson.

Additional benchmark images from the original source illustrate these performance differences.

The author also notes that orjson uses Rust rather than C/C++, yet achieves comparable speed, making it a compelling choice. However, orjson 's dumps() returns bytes and does not accept the indent parameter; formatting requires decoding the bytes, e.g.:

import orjson
json_str = orjson.dumps(record, option=orjson.OPT_INDENT_2).decode()

A known bug in ujson versions 3.0.0 and 3.1.0 causes the indent argument to be ignored for values greater than 1, leading the author to recommend downgrading to ujson==2.0.3 for correct indentation support, though that version cannot serialize datetime objects.

Overall, the article concludes that while ujson is a solid fast alternative, orjson offers superior performance and should be preferred when speed is critical.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

serializationJSONRapidJSONujsonorjson
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.