td-ameritrade-python-api icon indicating copy to clipboard operation
td-ameritrade-python-api copied to clipboard

JSON Serialization Performance Improvement

Open Dro92 opened this issue 3 years ago • 2 comments

Potentially replace response.json() with response.ujson() for serailizing the requets into a dictionary?

https://github.com/areed1192/td-ameritrade-python-api/blob/ab9352aa563e12e34f1a9524f3fd11c51975e065/td/client.py#L654

Dro92 avatar Apr 07 '21 15:04 Dro92

Interesting, I guess what is the big difference between json() and usjon()? I've never header of ujson() so it's more out of curiosity. 😄

areed1192 avatar Apr 11 '21 02:04 areed1192

The primary difference is ultra JSON (ujson) is written in C and has Python bindings which makes the encoding/decoding of JSON much faster. I've been testing it on my end with noticeable increases in speed, particularly for the SPY ETF option chain which has arguabaly the most contracts (9000+ contracts each with several key:values)

Your API works so well I figured this is something that could only improve it. I could probably post some of my performacne time tags, but the below two articles, I think, will shed a much greater light on the performance increase. The good thing is that ujson is also a drop in replacement for JSON (unlike some of the other encode/decode formats covered in the articles).

https://medium.com/dataweave/json-vs-simplejson-vs-ujson-2887b2c128b2 https://medium.com/@shmulikamar/python-serialization-benchmarks-8e5bb700530b

Dro92 avatar Apr 11 '21 16:04 Dro92