A quick benchmark:
import random from time import perf_counter_ns from orjson import dumps from msgspec.json import encode N = 10000 M = 2000 random.seed(42) for header, scale, func in [ ("Small Integers:", 1000, random.randint), ("Large Integers:", int(1e16), random.randint), ("Small Floats:", 1000, random.uniform), ("Large Floats:", int(1e16), random.uniform), ]: print(header) data = [func(-scale, scale) for _ in range(N)] start = perf_counter_ns() for _ in range(M): encode(data) stop = perf_counter_ns() print(f"- msgspec: {(stop - start) / (1000 * M):.2f} us") start = perf_counter_ns() for _ in range(M): dumps(data) stop = perf_counter_ns() print(f"- orjson: {(stop - start) / (1000 * M):.2f} us")
Before
$ python bench.py
Small Integers:
- msgspec: 122.89 us
- orjson: 137.58 us
Large Integers:
- msgspec: 228.17 us
- orjson: 376.78 us
Small Floats:
- msgspec: 374.01 us
- orjson: 355.98 us
Large Floats:
- msgspec: 404.31 us
- orjson: 395.41 us
This PR
$ python bench.py
Small Integers:
- msgspec: 132.78 us
- orjson: 143.62 us
Large Integers:
- msgspec: 189.28 us
- orjson: 377.78 us
Small Floats:
- msgspec: 351.13 us
- orjson: 356.19 us
Large Floats:
- msgspec: 391.72 us
- orjson: 395.72 us