django/django — pull request #53 by etianen (2012-05-10 11:30:11+00:00)
Title: Using Unicode.translate to speed up html and js escaping

Django is currently making use of inefficient repeated called to .replace() to perform html and js string escaping. Given that these functions are used all over the codebase, and typically called a hundred or so times per request, this is a sweet spot for some simple optimisation.

By replacing the repeated .replace() calls with a single call to .translate(), we improve efficiency with no side-effects (so far as I can see). All tests pass, certainly.

A discussion on the relative benefits of .replace() versus .translate() can be found here:

http://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python

(Benchmarks in the top answer)