Python 2.7 on App Engine, simplejson vs native json, who's faster?
Asked Answered
C

2

9

I've had the understanding that simplejson is much faster than the native json in Python, such as this thread: What are the differences between json and simplejson Python modules?

However, I was just thrown for a loop when I read in App Engines documentation that with Python 2.7

Uses the native JSON library, which is much faster than simplejson.

http://code.google.com/appengine/docs/python/python27/newin27.html

So now I'm confused. Everywhere else it seems to say simplejson is better, but now App Engine with Python 2.7 says the native is faster. What gives?

Catchpenny answered 19/11, 2011 at 4:39 Comment(1)
It's possible that the version that ships with Python 2.7 includes the optimizations of simplejson. It's also possible that Google is using some native implementation instead of the default module (sort of like how they rename cPickle to pickle). Mere speculation on my part, though ;-)Cubit
D
23

Before the release of the Python 2.7 runtime, nearly every module included with App Engine, and literally every module you could include yourself were pure python. With the 2.7 release, the json module includes speedups written in C, making it much faster than any simplejson you can run on App Engine.

The benefits to using simplejson on 2.7 you get normally (mainly having a version that's newer than it was when the latest release of Python 2.7 was made) don't apply, since you can't compile the speedups in the latest version and deploy them to App Engine.

Decoder answered 19/11, 2011 at 4:45 Comment(3)
I'm working on a project for Google, and they asked us to use simplejson for security reasons : because "it encodes '<' to '\u003c', and '>' to '\u003e' which helps prevent content sniffing XSS attacks (code.google.com/p/browsersec/wiki/…)"Allyson
@Philoozushi: that's a link to a 5-year-old handbook written before simplejson was added to the Python stdlib as "json". They're literally the same codebase and produce the same output (and neither of them do that encoding).Decoder
Indeed you're right! I was just relaying this information from Google, but I didn't get time to check it, sorry. Thanks for the precision!Allyson
P
4

I found myself forced to do a straight import json when I migrated to python 2.7. In my app I had to change from simplejson to this. You might find it generally recommendable to keep maximum compatibility with your "main component" and I consider python 2.7 one of my project's main uses / components where the others are Jinja2, WTForms and the i18n translations.

Pontus answered 19/11, 2011 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.