ultrajson
ultrajson copied to clipboard
Performance when deserializing a deeply nested object
I am dealing with large (1-3 MB), deeply nested structures that I'm communicating across web services. So I wanted to check if anything can be gained by using ujson. I ran the below code in CPython 3.6 with this object (result.json.gz) and was a bit surprised to find ujson being slower. The code is from an IPython session.
import json
import ujson
with open("result.json") as f:
content = f.read()
%timeit json.loads(content)
4.28 ms ± 86.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit ujson.loads(content)
6.09 ms ± 116 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
I just tested this with current versions (CPython 3.10.1 and ujson 5.4.0). On my machine, the two have virtually identical timings. So it's better now but still not great since ujson should really beat json easily...
$ python3 -m timeit -s 'import ujson as json'$'\n''with open("result.json") as f:'$'\n'' content = f.read()' 'json.loads(content)'
50 loops, best of 5: 5.86 msec per loop
$ python3 -m timeit -s 'import json'$'\n''with open("result.json") as f:'$'\n'' content = f.read()' 'json.loads(content)'
50 loops, best of 5: 5.91 msec per loop