Your API's slowest line isn't the database. It's json.dumps. I just benchmarke… · @ReignSwords · TwKit
@ReignSwords
Your API's slowest line isn't the database. It's json.dumps. I just benchmarked Python's JSON libs on a real 29.5MB payload. 60,000 records. Same machine. Best of 5 runs. Parsing it: - stdlib json.loads — 437ms - ujson — 314ms - orjson — 275ms - msgspec — 278ms Writing it back out: - stdlib json.dumps — 271ms - ujson — 178ms - orjson — 91ms - msgspec — 108ms The write path is the story. orjson serializes 3x faster than stdlib. On the read path it's only 1.6x. Three things the docs bury: 1. orjson.dumps() returns bytes, not a string. Forget the .decode() and your logs fill up with b'...' forever. 2. ujson barely beats stdlib anymore. "Just pip install ujson" is 2019 advice. The gap closed. 3. ensure_ascii=False made stdlib dumps SLOWER on my box (300ms vs 271ms). The classic performance tip backfires. My recipe, steal it: - Serializing API responses → orjson. FastAPI ships an ORJSONResponse class. One import, done. - Parsing into known shapes → msgspec with a Struct. - Pretty-printing for humans → stdlib. Keep it for the debugger. One pip install. 3x faster on the write path. So: team orjson, team msgspec, or team "stdlib was fine in 2019"? Pick a side. I benchmark the boring stuff so you don't have to — follow along if that's your thing. 🔵 Fellow Premium members, let's connect! I post about AI & tech — follow for more 👥
曝光 56 · 评论 6 · 点赞 2 · 书签 0
查看原帖