#30399: Use Python stdlib html.escape() to in django.utils.html.escape()
------------------------------------------------+------------------------
               Reporter:  Jon Dufresne          |          Owner:  nobody
                   Type:  Cleanup/optimization  |         Status:  new
              Component:  Utilities             |        Version:  master
               Severity:  Normal                |       Keywords:
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  0
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 The function `django.utils.html.escape()` partially duplicates the Python
 stdlib function `html.escape()`. We can replace this duplication with
 wider community developed version.

 `html.escape()` has been available since Python 3.2:

 https://docs.python.org/3/library/html.html#html.escape

 This function is also faster than Django's. As Python bug
 https://bugs.python.org/issue18020 concludes, using `.replace()` can be
 faster than `.translate()`. This function gets called numerous times when
 rendering templates. After making the change locally, I saw the following
 improvement:

 master:

 {{{
 $ python -m timeit -s 'from django.utils.html import escape'
 'escape(copyright)'
 50000 loops, best of 5: 4.03 usec per loop
 }}}

 branch:

 {{{
 $ python -m timeit -s 'from django.utils.html import escape'
 'escape(copyright)'
 100000 loops, best of 5: 2.45 usec per loop
 }}}

 One small concern, `html.escape()` converts `'` to `&#x27` rather than
 `&#39`. These values are functionally equivalent HTML, but I'll mention it
 as a backwards incompatible change as the literal text has changed

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30399>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.effa5cf8d81ac450745569a072843a7e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to