Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 25, 2025

📄 7% (0.07x) speedup for strip_spaces_between_tags in django/utils/html.py

⏱️ Runtime : 810 microseconds 757 microseconds (best of 188 runs)

📝 Explanation and details

The optimization precompiles the regular expression pattern r">\s+<" into a module-level variable _strip_spaces_pattern instead of compiling it on every function call.

What changed:

  • Added _strip_spaces_pattern = re.compile(r">\s+<") at module level
  • Changed re.sub(r">\s+<", "><", str(value)) to _strip_spaces_pattern.sub("><", str(value))

Why it's faster:
Regular expression compilation is expensive in Python. The original code recompiled the same pattern on every function call, while the optimized version compiles it once when the module is imported and reuses the compiled pattern object. This eliminates the regex compilation overhead from the critical path.

Performance characteristics:
The optimization shows consistent 15-30% speedups across most test cases, with particularly strong gains on simple cases (28-38% faster for empty strings, no tags, etc.) where regex compilation overhead dominates the total runtime. For larger-scale tests with many substitutions, the gains are more modest (1-4%) since the substitution work itself becomes the bottleneck, but the compilation savings still provide meaningful improvement.

This optimization is especially valuable in web frameworks like Django where HTML processing functions are called frequently during request handling.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 75 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import re

# imports
import pytest  # used for our unit tests
from django.utils.html import strip_spaces_between_tags

# unit tests

# =========================
# BASIC TEST CASES
# =========================

def test_basic_single_space_between_tags():
    # Single space between tags should be removed
    html = "<div> </div>"
    expected = "<div> </div>"
    codeflash_output = strip_spaces_between_tags(html) # 4.70μs -> 4.11μs (14.5% faster)

def test_basic_multiple_spaces_between_tags():
    # Multiple spaces between tags should be removed
    html = "<div>   <span>Text</span>   </div>"
    expected = "<div><span>Text</span></div>"
    codeflash_output = strip_spaces_between_tags(html) # 3.83μs -> 3.19μs (20.0% faster)

def test_basic_newline_between_tags():
    # Newline between tags should be removed
    html = "<div>\n<span>Text</span>\n</div>"
    expected = "<div><span>Text</span></div>"
    codeflash_output = strip_spaces_between_tags(html) # 3.50μs -> 2.92μs (19.8% faster)

def test_basic_tab_between_tags():
    # Tab between tags should be removed
    html = "<div>\t<span>Text</span>\t</div>"
    expected = "<div><span>Text</span></div>"
    codeflash_output = strip_spaces_between_tags(html) # 3.34μs -> 2.64μs (26.3% faster)

def test_basic_mixed_whitespace_between_tags():
    # Mixed whitespace (space, tab, newline) between tags should be removed
    html = "<div> \n\t <span>Text</span> \t\n </div>"
    expected = "<div><span>Text</span></div>"
    codeflash_output = strip_spaces_between_tags(html) # 3.34μs -> 2.62μs (27.4% faster)

def test_basic_no_spaces_between_tags():
    # No spaces between tags, output should be unchanged
    html = "<div><span>Text</span></div>"
    expected = "<div><span>Text</span></div>"
    codeflash_output = strip_spaces_between_tags(html) # 2.77μs -> 2.16μs (28.6% faster)

def test_basic_text_between_tags():
    # Spaces within text content should not be removed
    html = "<div> Hello <span>World</span> ! </div>"
    expected = "<div> Hello <span>World</span> ! </div>"
    codeflash_output = strip_spaces_between_tags(html) # 2.73μs -> 2.21μs (23.5% faster)

# =========================
# EDGE TEST CASES
# =========================

def test_edge_empty_string():
    # Empty string should return empty string
    html = ""
    expected = ""
    codeflash_output = strip_spaces_between_tags(html) # 2.37μs -> 1.80μs (31.7% faster)

def test_edge_only_spaces():
    # String of only spaces should remain unchanged
    html = "     "
    expected = "     "
    codeflash_output = strip_spaces_between_tags(html) # 2.45μs -> 1.89μs (29.9% faster)

def test_edge_no_tags():
    # String with no tags should remain unchanged
    html = "Just some text."
    expected = "Just some text."
    codeflash_output = strip_spaces_between_tags(html) # 2.46μs -> 1.78μs (37.5% faster)

def test_edge_spaces_inside_tags():
    # Spaces inside tags should not be removed
    html = "<div class='foo' > <span id='bar' >Text</span> </div>"
    expected = "<div class='foo' ><span id='bar' >Text</span></div>"
    codeflash_output = strip_spaces_between_tags(html) # 3.46μs -> 2.81μs (23.2% faster)

def test_edge_spaces_between_tags_and_text():
    # Spaces between tags and text should not be removed
    html = "<div>   Text   <span>   More Text   </span>   </div>"
    expected = "<div>   Text   <span>   More Text   </span>   </div>"
    codeflash_output = strip_spaces_between_tags(html) # 3.28μs -> 2.70μs (21.3% faster)

def test_edge_multiple_adjacent_tag_pairs():
    # Multiple adjacent tag pairs with spaces between them
    html = "<a> </a> <b> </b> <c> </c>"
    expected = "<a> </a><b> </b><c> </c>"
    codeflash_output = strip_spaces_between_tags(html) # 3.72μs -> 3.11μs (19.6% faster)

def test_edge_unicode_whitespace_between_tags():
    # Unicode whitespace (e.g., non-breaking space) between tags should NOT be removed
    html = "<div>\u00A0<span>Text</span>\u00A0</div>"
    # The regex only matches \s, which includes non-breaking space, so it will be removed
    expected = "<div><span>Text</span></div>"
    codeflash_output = strip_spaces_between_tags(html) # 3.62μs -> 2.99μs (21.2% faster)

def test_edge_non_string_input():
    # Non-string input should be converted to string
    html = 12345
    expected = "12345"
    codeflash_output = strip_spaces_between_tags(html) # 2.50μs -> 1.92μs (30.4% faster)

def test_edge_nested_tags_with_spaces():
    # Nested tags with spaces between them
    html = "<div>  <span> <em>Text</em> </span>  </div>"
    expected = "<div><span><em>Text</em></span></div>"
    codeflash_output = strip_spaces_between_tags(html) # 3.71μs -> 3.09μs (19.8% faster)

def test_edge_self_closing_tags():
    # Self-closing tags with spaces between them
    html = "<br />   <hr />   <img src='x' />"
    expected = "<br /><hr /><img src='x' />"
    codeflash_output = strip_spaces_between_tags(html) # 3.22μs -> 2.62μs (22.6% faster)

def test_edge_tags_with_attributes_and_spaces():
    # Tags with attributes and spaces between them
    html = "<div class='a'>   <span id='b'>Text</span>   </div>"
    expected = "<div class='a'><span id='b'>Text</span></div>"
    codeflash_output = strip_spaces_between_tags(html) # 3.23μs -> 2.72μs (19.0% faster)

def test_edge_tags_with_comments_between():
    # Spaces between tags and HTML comments should be removed
    html = "<div> <!-- comment --> <span>Text</span> </div>"
    expected = "<div><!-- comment --><span>Text</span></div>"
    codeflash_output = strip_spaces_between_tags(html) # 3.43μs -> 2.85μs (20.3% faster)

def test_edge_multiple_types_of_whitespace():
    # Multiple types of whitespace between tags
    html = "<div>\n\t  <span>Text</span>\t\n  </div>"
    expected = "<div><span>Text</span></div>"
    codeflash_output = strip_spaces_between_tags(html) # 3.19μs -> 2.62μs (21.6% faster)

def test_edge_tags_with_cdata_between():
    # Spaces between tags and CDATA should be removed
    html = "<script>  <![CDATA[alert('x');]]>  </script>"
    expected = "<script><![CDATA[alert('x');]]></script>"
    codeflash_output = strip_spaces_between_tags(html) # 3.20μs -> 2.53μs (26.5% faster)

def test_edge_tags_with_doctype():
    # Spaces between DOCTYPE and html tag
    html = "<!DOCTYPE html>   <html>   <body></body>   </html>"
    expected = "<!DOCTYPE html><html><body></body></html>"
    codeflash_output = strip_spaces_between_tags(html) # 3.47μs -> 2.83μs (22.6% faster)

# =========================
# LARGE SCALE TEST CASES
# =========================

def test_large_scale_many_tags():
    # Test with a large number of tags and spaces between them
    html = "".join([f"<div>   </div>   " for _ in range(500)])
    expected = "".join([f"<div>   </div>" for _ in range(500)])
    # All spaces between tags should be removed
    codeflash_output = strip_spaces_between_tags(html) # 82.5μs -> 74.6μs (10.6% faster)

def test_large_scale_long_text_between_tags():
    # Test with long text between tags, ensure only spaces between tags are removed
    html = "<div>" + " " * 500 + "<span>" + "a" * 500 + "</span>" + " " * 500 + "</div>"
    expected = "<div>" + " " * 500 + "<span>" + "a" * 500 + "</span>" + " " * 500 + "</div>"
    codeflash_output = strip_spaces_between_tags(html) # 5.46μs -> 4.82μs (13.4% faster)

def test_large_scale_mixed_tags_and_text():
    # Large HTML with mixed tags and text, spaces only between tags should be removed
    html = "".join([f"<p>  Text {i}  </p>   " for i in range(300)])
    expected = "".join([f"<p>  Text {i}  </p>" for i in range(300)])
    codeflash_output = strip_spaces_between_tags(html) # 31.3μs -> 30.6μs (2.25% faster)

def test_large_scale_nested_tags():
    # Large number of nested tags with spaces between them
    html = ""
    expected = ""
    for i in range(100):
        html += "<div>   "
        expected += "<div>"
    for i in range(100):
        html += "</div>   "
        expected += "</div>"
    codeflash_output = strip_spaces_between_tags(html) # 18.4μs -> 17.5μs (4.77% faster)

def test_large_scale_tags_with_varied_whitespace():
    # Large HTML with varied whitespace between tags
    html = "".join([f"<span>\n\t  </span>\n   " for _ in range(400)])
    expected = "".join([f"<span>\n\t  </span>" for _ in range(400)])
    codeflash_output = strip_spaces_between_tags(html) # 61.0μs -> 61.1μs (0.196% slower)

# =========================
# ADDITIONAL EDGE CASES
# =========================

def test_edge_single_tag():
    # Single tag, no spaces to strip
    html = "<div>"
    expected = "<div>"
    codeflash_output = strip_spaces_between_tags(html) # 2.74μs -> 2.13μs (28.5% faster)

def test_edge_tag_with_space_before_closing():
    # Space before closing tag should not be stripped
    html = "<div >"
    expected = "<div >"
    codeflash_output = strip_spaces_between_tags(html) # 2.63μs -> 2.04μs (29.4% faster)

def test_edge_tag_with_space_after_opening():
    # Space after opening tag should not be stripped
    html = "<div> "
    expected = "<div> "
    codeflash_output = strip_spaces_between_tags(html) # 2.60μs -> 2.04μs (27.5% faster)

def test_edge_tag_with_space_before_opening():
    # Space before opening tag should not be stripped
    html = " <div>"
    expected = " <div>"
    codeflash_output = strip_spaces_between_tags(html) # 2.56μs -> 1.98μs (29.5% faster)

def test_edge_tag_with_space_after_closing():
    # Space after closing tag should not be stripped
    html = "</div> "
    expected = "</div> "
    codeflash_output = strip_spaces_between_tags(html) # 2.53μs -> 1.99μs (27.2% faster)

def test_edge_multiple_spaces_between_tags():
    # Multiple spaces between tags should be removed
    html = "<a>     <b>     <c>     </c>     </b>     </a>"
    expected = "<a><b><c></c></b></a>"
    codeflash_output = strip_spaces_between_tags(html) # 3.71μs -> 3.06μs (21.2% faster)

def test_edge_tags_with_javascript():
    # Spaces between script tags should be removed, but not inside JS
    html = "<script> var x = 1; </script>   <script> var y = 2; </script>"
    expected = "<script> var x = 1; </script><script> var y = 2; </script>"
    codeflash_output = strip_spaces_between_tags(html) # 3.16μs -> 2.60μs (21.7% faster)

def test_edge_tags_with_style():
    # Spaces between style tags should be removed, but not inside CSS
    html = "<style> .foo { margin: 0; } </style>   <style> .bar { padding: 0; } </style>"
    expected = "<style> .foo { margin: 0; } </style><style> .bar { padding: 0; } </style>"
    codeflash_output = strip_spaces_between_tags(html) # 3.10μs -> 2.52μs (22.7% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import re

# imports
import pytest  # used for our unit tests
from django.utils.html import strip_spaces_between_tags

# unit tests

# 1. Basic Test Cases

def test_basic_single_space_between_tags():
    # Basic: single space between tags
    html = "<div> </div>"
    # No space between tags, so nothing to remove
    codeflash_output = strip_spaces_between_tags(html) # 2.98μs -> 2.33μs (28.0% faster)

def test_basic_multiple_spaces_between_tags():
    # Basic: multiple spaces between tags
    html = "<div>   </div>"
    # No space between tags, so nothing to remove
    codeflash_output = strip_spaces_between_tags(html) # 2.96μs -> 2.27μs (30.6% faster)

def test_basic_space_between_tags():
    # Basic: space between tags
    html = "<div></div> <span></span>"
    # Space between tags should be removed
    codeflash_output = strip_spaces_between_tags(html) # 2.98μs -> 2.42μs (22.9% faster)

def test_basic_newline_between_tags():
    # Basic: newline between tags
    html = "<div></div>\n<span></span>"
    # Newline between tags should be removed
    codeflash_output = strip_spaces_between_tags(html) # 2.98μs -> 2.35μs (27.0% faster)

def test_basic_tabs_between_tags():
    # Basic: tab between tags
    html = "<div></div>\t<span></span>"
    # Tab between tags should be removed
    codeflash_output = strip_spaces_between_tags(html) # 2.93μs -> 2.32μs (26.2% faster)

def test_basic_mixed_whitespace_between_tags():
    # Basic: mixed whitespace between tags
    html = "<div></div> \n\t <span></span>"
    # All whitespace between tags should be removed
    codeflash_output = strip_spaces_between_tags(html) # 2.93μs -> 2.32μs (26.1% faster)

def test_basic_nested_tags_with_spaces():
    # Basic: nested tags with spaces between them
    html = "<div> <span>Text</span> </div>"
    # Only spaces between tags are removed, not inside tags
    codeflash_output = strip_spaces_between_tags(html) # 3.24μs -> 2.69μs (20.5% faster)

def test_basic_multiple_tag_pairs():
    # Basic: multiple tag pairs with spaces between them
    html = "<a></a>   <b></b>   <c></c>"
    codeflash_output = strip_spaces_between_tags(html) # 3.30μs -> 2.64μs (24.8% faster)

def test_basic_no_spaces_between_tags():
    # Basic: no spaces between tags
    html = "<div></div><span></span>"
    codeflash_output = strip_spaces_between_tags(html) # 2.65μs -> 2.09μs (26.7% faster)

def test_basic_text_between_tags():
    # Basic: text between tags, should not be removed
    html = "<div>hello</div> <span>world</span>"
    codeflash_output = strip_spaces_between_tags(html) # 3.05μs -> 2.43μs (25.5% faster)

# 2. Edge Test Cases

def test_edge_empty_string():
    # Edge: empty string
    html = ""
    codeflash_output = strip_spaces_between_tags(html) # 2.33μs -> 1.68μs (38.3% faster)

def test_edge_no_tags():
    # Edge: string with no tags
    html = "just some text"
    codeflash_output = strip_spaces_between_tags(html) # 2.40μs -> 1.84μs (30.4% faster)

def test_edge_only_spaces():
    # Edge: string with only spaces
    html = "     "
    codeflash_output = strip_spaces_between_tags(html) # 2.33μs -> 1.73μs (34.3% faster)

def test_edge_only_tags_no_spaces():
    # Edge: only tags, no spaces
    html = "<a></a><b></b>"
    codeflash_output = strip_spaces_between_tags(html) # 2.65μs -> 2.10μs (26.6% faster)

def test_edge_only_tags_with_spaces_between():
    # Edge: only tags, spaces between
    html = "<a></a>   <b></b>"
    codeflash_output = strip_spaces_between_tags(html) # 3.06μs -> 2.52μs (21.4% faster)

def test_edge_tags_with_attributes_and_spaces():
    # Edge: tags with attributes and spaces between tags
    html = "<div class='foo'></div>   <span id='bar'></span>"
    codeflash_output = strip_spaces_between_tags(html) # 3.10μs -> 2.46μs (26.2% faster)

def test_edge_tags_with_attributes_and_internal_spaces():
    # Edge: tags with attributes and internal spaces
    html = "<div class='foo'>   </div>   <span id='bar'> </span>"
    # Only spaces between tags should be removed
    codeflash_output = strip_spaces_between_tags(html) # 3.40μs -> 2.87μs (18.3% faster)

def test_edge_self_closing_tags_with_spaces():
    # Edge: self-closing tags with spaces between
    html = "<img src='foo.jpg' />   <br />"
    codeflash_output = strip_spaces_between_tags(html) # 2.94μs -> 2.36μs (24.5% faster)

def test_edge_multiple_whitespace_characters_between_tags():
    # Edge: multiple whitespace characters (space, tab, newline) between tags
    html = "<div></div> \t\n <span></span>"
    codeflash_output = strip_spaces_between_tags(html) # 3.20μs -> 2.56μs (25.2% faster)

def test_edge_unicode_whitespace_between_tags():
    # Edge: unicode whitespace between tags
    html = "<div></div>\u2003<span></span>"
    # Only ASCII whitespace is matched by \s, so this should be removed
    codeflash_output = strip_spaces_between_tags(html) # 4.22μs -> 3.68μs (14.7% faster)

def test_edge_non_string_input_int():
    # Edge: non-string input (int)
    html = 12345
    # Should convert to string and do nothing
    codeflash_output = strip_spaces_between_tags(html) # 2.50μs -> 1.95μs (28.4% faster)

def test_edge_non_string_input_list():
    # Edge: non-string input (list)
    html = ["<div></div>", "<span></span>"]
    # Should convert to string and do nothing
    codeflash_output = strip_spaces_between_tags(html) # 4.08μs -> 3.47μs (17.6% faster)

def test_edge_tag_like_text():
    # Edge: text that looks like a tag but isn't
    html = "foo > bar < baz"
    # Should not remove anything
    codeflash_output = strip_spaces_between_tags(html) # 2.61μs -> 2.03μs (28.6% faster)

def test_edge_spaces_inside_tag_names():
    # Edge: spaces inside tag names (invalid HTML, but test anyway)
    html = "<div ></div > <span ></span >"
    # Only spaces between tags should be removed
    codeflash_output = strip_spaces_between_tags(html) # 3.05μs -> 2.54μs (20.0% faster)

def test_edge_comment_between_tags():
    # Edge: HTML comment between tags with spaces
    html = "<div></div> <!-- comment --> <span></span>"
    # Only spaces between tags are removed, not inside comments
    codeflash_output = strip_spaces_between_tags(html) # 3.40μs -> 2.67μs (27.2% faster)

def test_edge_doctype_and_spaces():
    # Edge: DOCTYPE with spaces between tags
    html = "<!DOCTYPE html>   <html></html>"
    codeflash_output = strip_spaces_between_tags(html) # 3.03μs -> 2.35μs (29.1% faster)

def test_edge_spaces_between_close_and_open_tags():
    # Edge: spaces between closing and opening tags
    html = "</div>   <span>"
    codeflash_output = strip_spaces_between_tags(html) # 2.98μs -> 2.33μs (27.7% faster)

def test_edge_spaces_between_open_and_close_tags():
    # Edge: spaces between opening and closing tags
    html = "<div>   </span>"
    # Not between tags, so nothing should be removed
    codeflash_output = strip_spaces_between_tags(html) # 2.93μs -> 2.30μs (27.1% faster)

def test_edge_spaces_between_tags_with_text():
    # Edge: spaces between tags with text in between
    html = "<div>foo</div>   <span>bar</span>"
    codeflash_output = strip_spaces_between_tags(html) # 3.03μs -> 2.46μs (23.1% faster)

def test_edge_multiple_tag_types():
    # Edge: multiple tag types with spaces between
    html = "<ul>\n  <li>Item</li> \n  <li>Item2</li> \n</ul>"
    # Only spaces between tags should be removed, not inside tags
    codeflash_output = strip_spaces_between_tags(html) # 3.43μs -> 2.93μs (17.1% faster)

# 3. Large Scale Test Cases

def test_large_scale_many_tags():
    # Large scale: 1000 tags with spaces between
    html = "".join(f"<div>{i}</div>   " for i in range(1000))
    expected = "".join(f"<div>{i}</div>" for i in range(1000))
    codeflash_output = strip_spaces_between_tags(html) # 83.6μs -> 84.2μs (0.676% slower)

def test_large_scale_long_whitespace_between_tags():
    # Large scale: long whitespace between tags
    html = "<a></a>" + (" " * 500) + "<b></b>"
    codeflash_output = strip_spaces_between_tags(html) # 3.96μs -> 3.38μs (17.2% faster)

def test_large_scale_mixed_tags_and_text():
    # Large scale: many tags and text with spaces between tags
    html = "".join(f"<p>{i}</p>   Some text   " for i in range(500))
    # Only spaces between tags are removed, not inside tags or text
    expected = "".join(f"<p>{i}</p>   Some text   " for i in range(500))
    codeflash_output = strip_spaces_between_tags(html) # 19.0μs -> 18.2μs (4.73% faster)

def test_large_scale_nested_tags_with_spaces():
    # Large scale: nested tags with spaces between
    html = "".join(f"<div><span>{i}</span></div>   " for i in range(500))
    expected = "".join(f"<div><span>{i}</span></div>" for i in range(500))
    codeflash_output = strip_spaces_between_tags(html) # 54.8μs -> 53.9μs (1.70% faster)

def test_large_scale_tags_with_newlines_between():
    # Large scale: tags with newlines and spaces between
    html = "".join(f"<div>{i}</div>\n   " for i in range(500))
    expected = "".join(f"<div>{i}</div>" for i in range(500))
    codeflash_output = strip_spaces_between_tags(html) # 45.5μs -> 44.6μs (2.15% faster)

def test_large_scale_tags_with_tabs_between():
    # Large scale: tags with tabs between
    html = "".join(f"<div>{i}</div>\t" for i in range(500))
    expected = "".join(f"<div>{i}</div>" for i in range(500))
    codeflash_output = strip_spaces_between_tags(html) # 43.4μs -> 42.4μs (2.51% faster)

def test_large_scale_tags_with_mixed_whitespace_between():
    # Large scale: tags with mixed whitespace between
    html = "".join(f"<div>{i}</div> \n\t " for i in range(500))
    expected = "".join(f"<div>{i}</div>" for i in range(500))
    codeflash_output = strip_spaces_between_tags(html) # 46.2μs -> 44.6μs (3.47% faster)

def test_large_scale_tags_with_attributes_and_spaces():
    # Large scale: tags with attributes and spaces between tags
    html = "".join(f"<div id='{i}'></div>   " for i in range(500))
    expected = "".join(f"<div id='{i}'></div>" for i in range(500))
    codeflash_output = strip_spaces_between_tags(html) # 44.8μs -> 44.5μs (0.669% faster)

def test_large_scale_self_closing_tags_with_spaces():
    # Large scale: self-closing tags with spaces between
    html = "".join(f"<img src='foo{i}.jpg' />   " for i in range(500))
    expected = "".join(f"<img src='foo{i}.jpg' />" for i in range(500))
    codeflash_output = strip_spaces_between_tags(html) # 39.9μs -> 38.9μs (2.45% faster)

def test_large_scale_tags_with_text_and_spaces_between_tags():
    # Large scale: tags with text and spaces between tags
    html = "".join(f"<div>{i} text</div>   " for i in range(500))
    expected = "".join(f"<div>{i} text</div>" for i in range(500))
    codeflash_output = strip_spaces_between_tags(html) # 44.8μs -> 44.4μs (0.815% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-strip_spaces_between_tags-mh6sgosd and push.

Codeflash

The optimization precompiles the regular expression pattern `r">\s+<"` into a module-level variable `_strip_spaces_pattern` instead of compiling it on every function call.

**What changed:**
- Added `_strip_spaces_pattern = re.compile(r">\s+<")` at module level
- Changed `re.sub(r">\s+<", "><", str(value))` to `_strip_spaces_pattern.sub("><", str(value))`

**Why it's faster:**
Regular expression compilation is expensive in Python. The original code recompiled the same pattern on every function call, while the optimized version compiles it once when the module is imported and reuses the compiled pattern object. This eliminates the regex compilation overhead from the critical path.

**Performance characteristics:**
The optimization shows consistent 15-30% speedups across most test cases, with particularly strong gains on simple cases (28-38% faster for empty strings, no tags, etc.) where regex compilation overhead dominates the total runtime. For larger-scale tests with many substitutions, the gains are more modest (1-4%) since the substitution work itself becomes the bottleneck, but the compilation savings still provide meaningful improvement.

This optimization is especially valuable in web frameworks like Django where HTML processing functions are called frequently during request handling.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 25, 2025 21:25
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Oct 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant