Why is json.loads an order of magnitude faster than ast.literal_eval?
Solution 1:
The two functions are parsing entirely different languages—JSON, and Python literal syntax.* As literal_eval
says:
The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and
None
.
JSON, by contrast, only handles double-quoted JavaScript string literals (not quite identical to Python's**), JavaScript numbers (only int and float***), objects (roughly equivalent to dicts), arrays (roughly equivalent to lists), JavaScript booleans (which are different from Python's), and null
.
The fact that these two languages happen to have some overlap doesn't mean they're the same language.
Why is
json.loads
so much faster ?
Because Python literal syntax is a more complex and powerful language than JSON, it's likely to be slower to parse. And, probably more importantly, because Python literal syntax is not intended to be used as a data interchange format (in fact, it's specifically not supposed to be used for that), nobody is likely to put much effort into making it fast for data interchange.****
This question seems to imply that ast is more flexible regarding the input data (double or single quotes)
That, and raw string literals, and Unicode vs. bytes string literals, and complex numbers, and sets, and all kinds of other things JSON doesn't handle.
Are there use cases where I would prefer to use ast.literal_eval over json.loads although it's slower ?
Yes. When you want to parse Python literals, you should use ast.literal_eval
. (Or, better yet, re-think your design so you don't want to parse Python literals…)
* This is a bit of a vague term. For example, -2
is not a literal in Python, but an operator expression, but literal_eval
can handle it. And of course tuple/list/dict/set displays are not literals, but literal_eval
can handle them—except that comprehensions are also displays, and literal_eval
cannot handle them. Other functions in the ast
module can help you find out what really is and isn't a literal—e.g., ast.dump(ast.parse("expr"))
.
** For example, "\q"
is an error in JSON.
*** Technically, JSON only handles one "number" type, which is floating-point. But Python's json
module parses numbers with no decimal point or exponent as integers, and the same is true in many other languages' JSON modules.
**** If you missed Tim Peters's comment on the question: "ast.literal_eval
is so lightly used that nobody felt it was worth the time to work (& work, & work) at speeding it. In contrast, the JSON libraries are routinely used to parse gigabytes of data."