Skip to content

⚡️ Speed up function _extract_child_components by 83% in PR #1563 (codeflash/optimize-pr1561-2026-02-20T03.15.59)#1567

Merged
claude[bot] merged 4 commits intocodeflash/optimize-pr1561-2026-02-20T03.15.59from
codeflash/optimize-pr1563-2026-02-20T03.27.20
Feb 20, 2026
Merged

⚡️ Speed up function _extract_child_components by 83% in PR #1563 (codeflash/optimize-pr1561-2026-02-20T03.15.59)#1567
claude[bot] merged 4 commits intocodeflash/optimize-pr1561-2026-02-20T03.15.59from
codeflash/optimize-pr1563-2026-02-20T03.27.20

Conversation

@codeflash-ai
Copy link
Contributor

@codeflash-ai codeflash-ai bot commented Feb 20, 2026

⚡️ This pull request contains optimizations for PR #1563

If you approve this dependent PR, these changes will be merged into the original PR branch codeflash/optimize-pr1561-2026-02-20T03.15.59.

This PR will be automatically closed if the original PR is merged.


📄 83% (0.83x) speedup for _extract_child_components in codeflash/languages/javascript/frameworks/react/context.py

⏱️ Runtime : 2.26 milliseconds 1.23 milliseconds (best of 250 runs)

📝 Explanation and details

I replaced the per-match Match object creation (finditer + group(1)) with re.findall to get the group strings directly and removed matches in bulk via set operations. This reduces object allocations and Python-level loop overhead, giving measurable speed and memory improvements on large inputs while preserving behavior and output ordering.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 63 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Click to see Generated Regression Tests
import pytest  # used for our unit tests
from codeflash.languages.javascript.frameworks.react.context import \
    _extract_child_components
from codeflash.languages.javascript.treesitter import TreeSitterAnalyzer

# Note: _extract_child_components doesn't actually use the analyzer or full_source,
# but we create a real TreeSitterAnalyzer instance per the testing rules.
_ANALYZER = TreeSitterAnalyzer("javascript")

def test_single_child_component():
    # Very basic case: a single JSX component should be detected.
    src = "<Child />"
    # Call the function under test with a real analyzer instance and an empty full_source.
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 4.05μs -> 3.10μs (30.7% faster)

def test_multiple_components_and_sorting():
    # Multiple components in arbitrary order should be deduplicated and sorted.
    src = "<Z /><A /><M />"
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 5.10μs -> 3.57μs (43.0% faster)

def test_duplicates_are_deduplicated():
    # Repeated component occurrences should produce a single entry.
    src = "<A /><A></A><A />"
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 5.17μs -> 3.49μs (48.2% faster)

def test_namespace_dot_notation_allowed():
    # The regex allows dots in component names (e.g., Namespace.Component).
    src = "<Namespace.Component /><Other />"
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 4.76μs -> 3.58μs (33.0% faster)

def test_ignores_react_builtins():
    # Builtins listed in the filter must be ignored even though they match the regex.
    src = "<React.Fragment /><Fragment /><Suspense /><React.Suspense /><RealChild />"
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 5.83μs -> 4.48μs (30.2% faster)

def test_empty_source_returns_empty_list():
    # Empty input should yield an empty list.
    src = ""
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 2.32μs -> 1.60μs (45.0% faster)

def test_lowercase_html_tags_are_ignored():
    # HTML tags (starting with lowercase) do not match the regex and should be ignored.
    src = "<div /><span /><a />"
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 2.94μs -> 2.23μs (31.8% faster)

def test_nested_components_in_props_are_found():
    # JSX inside props (e.g., prop={<Child/>}) still contains '<Child' and should be picked up.
    src = "<Parent prop={<Child/>} /><Sibling />"
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 5.40μs -> 3.91μs (38.2% faster)

def test_component_names_with_numbers():
    # Component names can include digits after the initial capital letter.
    src = "<V2 /><Comp123/>"
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 4.78μs -> 3.49μs (37.1% faster)

def test_hyphen_in_tag_breaks_match_at_hyphen():
    # The regex does not include hyphens, so "<My-Comp />" will match only "My".
    # This verifies the current (documented) behavior rather than attempting to change it.
    src = "<My-Comp /><Another />"
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 4.71μs -> 3.36μs (40.3% faster)

def test_large_number_of_unique_components():
    # Large-scale test: generate 1000 unique component tags to exercise scalability.
    count = 1000
    parts = [f"<C{i} />" for i in range(count)]
    src = " ".join(parts)
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 457μs -> 268μs (70.7% faster)
    # Expected is the set of all names sorted lexicographically (as function returns sorted list).
    expected = sorted({f"C{i}" for i in range(count)})

def test_large_scale_with_many_duplicates():
    # Another large test: many duplicate occurrences combined with many unique names.
    unique_count = 500
    # 1000 duplicates of "Common" and 500 unique U0..U499
    duplicates = ["<Common />"] * 1000
    uniques = [f"<U{i} />" for i in range(unique_count)]
    src = " ".join(duplicates + uniques + duplicates)
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 829μs -> 384μs (116% faster)
    # Expect "Common" plus all U* names sorted.
    expected = sorted({"Common"} | {f"U{i}" for i in range(unique_count)})

def test_component_adjacent_to_text_and_attributes():
    # Components adjacent to text and attributes should still be matched correctly.
    src = "Text<CompA/>more <CompB prop='x'>inner</CompB> tail"
    codeflash_output = _extract_child_components(src, _ANALYZER, ""); result = codeflash_output # 5.43μs -> 4.01μs (35.5% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
import pytest
from codeflash.languages.javascript.frameworks.react.context import \
    _extract_child_components
from codeflash.languages.javascript.treesitter import TreeSitterAnalyzer

def test_empty_component_source():
    """Test with empty component source - should return empty list."""
    analyzer = TreeSitterAnalyzer("javascript")
    codeflash_output = _extract_child_components("", analyzer, ""); result = codeflash_output # 2.45μs -> 1.79μs (36.8% faster)

def test_no_jsx_in_source():
    """Test with source containing no JSX - should return empty list."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "const x = 5; function foo() { return 'hello'; }"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 2.41μs -> 1.74μs (38.5% faster)

def test_single_component():
    """Test with single uppercase component - should extract it."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "const App = () => <MyComponent />"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 4.22μs -> 3.29μs (28.0% faster)

def test_multiple_components():
    """Test with multiple different components - should extract all and sort."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Header><Footer><Card><Button></Button></Card></Footer></Header>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 6.18μs -> 4.18μs (48.0% faster)

def test_duplicate_components():
    """Test with duplicate component usage - should return each once."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<MyButton><MyButton><MyButton></MyButton></MyButton></MyButton>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.44μs -> 3.96μs (37.5% faster)

def test_filter_fragment():
    """Test that Fragment is filtered out."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Fragment><Header></Header></Fragment>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 4.88μs -> 3.70μs (32.0% faster)

def test_filter_react_fragment():
    """Test that React.Fragment is filtered out."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<React.Fragment><Header></Header></React.Fragment>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 4.94μs -> 3.65μs (35.4% faster)

def test_filter_suspense():
    """Test that Suspense is filtered out."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Suspense><LazyComponent></LazyComponent></Suspense>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.00μs -> 3.75μs (33.4% faster)

def test_filter_react_suspense():
    """Test that React.Suspense is filtered out."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<React.Suspense><LazyComponent></LazyComponent></React.Suspense>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.01μs -> 3.68μs (36.3% faster)

def test_lowercase_components_ignored():
    """Test that lowercase tags (HTML elements) are ignored."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<div><span><MyComponent></MyComponent></span></div>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 4.50μs -> 3.48μs (29.4% faster)

def test_component_with_dots():
    """Test component name with dot notation like Namespace.Component."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Utils.Button><Utils.Modal></Utils.Modal></Utils.Button>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.09μs -> 3.64μs (39.9% faster)

def test_component_with_numbers():
    """Test component names containing numbers."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Component1><Component2></Component2></Component1>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 4.78μs -> 3.67μs (30.4% faster)

def test_mixed_components_and_fragments():
    """Test mix of components and various fragment types."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = (
        "<div><Fragment><Header></Header></Fragment>"
        "<React.Fragment><Footer></Footer></React.Fragment>"
        "<Suspense><Lazy></Lazy></Suspense>"
        "<React.Suspense><AsyncComp></AsyncComp></React.Suspense></div>"
    )
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 7.96μs -> 5.73μs (39.0% faster)

def test_self_closing_tags():
    """Test with self-closing component tags."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Container><Card /> <Button /> <Icon /></Container>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.78μs -> 4.31μs (34.2% faster)

def test_components_with_attributes():
    """Test that component names are extracted even with attributes."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = '<MyComponent prop="value" className="foo"><ChildComponent id="test" /></MyComponent>'
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.00μs -> 3.64μs (37.4% faster)

def test_components_with_expressions():
    """Test components with JSX expressions in attributes."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = '<Card title={getTitleFn()}><Badge count={5} /><Icon name={iconName} /></Card>'
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.40μs -> 3.84μs (40.7% faster)

def test_nested_components():
    """Test deeply nested component structure."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = (
        "<App>"
        "  <Layout>"
        "    <Sidebar>"
        "      <Menu>"
        "        <Item />"
        "      </Menu>"
        "    </Sidebar>"
        "    <Content>"
        "      <Page />"
        "    </Content>"
        "  </Layout>"
        "</App>"
    )
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 7.75μs -> 5.37μs (44.4% faster)

def test_only_built_in_fragments():
    """Test when source contains only built-in fragments."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Fragment><React.Fragment><Suspense><React.Suspense></React.Suspense></Suspense></React.Fragment></Fragment>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.56μs -> 4.34μs (28.1% faster)

def test_component_case_sensitive():
    """Test that component matching is case-sensitive."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<MyComponent><mycomponent /><MYCOMPONENT /></MyComponent>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.15μs -> 3.76μs (37.1% faster)

def test_special_characters_in_component_names():
    """Test component names with valid identifier characters."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<MyComponent_Name><Component_2 /><_PrivateComponent /></MyComponent_Name>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.06μs -> 3.68μs (37.6% faster)

def test_whitespace_handling():
    """Test with various whitespace configurations."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Component   >  <Child1 />  \n  <Child2 />  </Component>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.47μs -> 3.88μs (41.1% faster)

def test_return_type_is_sorted_list():
    """Test that return value is always a sorted list."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Zebra><Apple><Banana /></Apple></Zebra>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.40μs -> 3.88μs (39.3% faster)

def test_empty_list_returns_empty_list():
    """Test that an empty input consistently returns an empty list."""
    analyzer = TreeSitterAnalyzer("javascript")
    for _ in range(5):
        codeflash_output = _extract_child_components("", analyzer, ""); result = codeflash_output # 5.36μs -> 3.68μs (45.8% faster)

def test_fragment_case_exact_match():
    """Test that Fragment filtering is exact (not substring-based)."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<FragmentWrapper><FragmentList><Fragment></Fragment></FragmentList></FragmentWrapper>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.69μs -> 4.25μs (34.0% faster)

def test_large_component_tree():
    """Test with many unique components - performance baseline."""
    analyzer = TreeSitterAnalyzer("javascript")
    # Create a large tree with 100 unique components
    components = [f"Component{i}" for i in range(100)]
    source = "".join(f"<{c}>" for c in components) + "".join(f"</{c}>" for c in reversed(components))
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 50.8μs -> 31.3μs (62.4% faster)

def test_many_duplicate_components():
    """Test with same component repeated many times."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<".join(["Button"] * 500) + ">" + ("<" * 500).join([""] * 1)
    # Simpler version: many repetitions of same tag
    source = "".join(f"<Button>" for _ in range(500)) + "".join(f"</Button>" for _ in range(500))
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 166μs -> 74.2μs (124% faster)

def test_mixed_case_names():
    """Test various valid naming patterns starting with uppercase."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<HTTPSConnection><DBConnection><MyHTTPSServer></MyHTTPSServer></DBConnection></HTTPSConnection>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.70μs -> 4.23μs (34.8% faster)

def test_single_letter_components():
    """Test single letter component names."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<A><B><C><D></D></C></B></A>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.68μs -> 4.01μs (41.8% faster)

def test_analyzer_parameter_is_used():
    """Test that analyzer parameter is accepted (even if not actively used by regex)."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Component1><Component2></Component2></Component1>"
    # Function should accept analyzer without error
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.17μs -> 3.72μs (39.1% faster)

def test_full_source_parameter_accepted():
    """Test that full_source parameter is accepted by the function."""
    analyzer = TreeSitterAnalyzer("javascript")
    component_source = "<MyComponent />"
    full_source = "import React from 'react';\nconst App = () => <MyComponent />"
    # Function should accept full_source without error
    codeflash_output = _extract_child_components(component_source, analyzer, full_source); result = codeflash_output # 4.01μs -> 3.03μs (32.5% faster)

def test_react_fragment_variant_casing():
    """Test that only exact 'React.Fragment' is filtered, not variations."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<React.Fragment><Header></Header></React.Fragment><ReactFragment><Footer></Footer></ReactFragment>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 6.27μs -> 4.43μs (41.6% faster)

def test_consistent_ordering():
    """Test that output is consistently ordered regardless of input order."""
    analyzer = TreeSitterAnalyzer("javascript")
    components = ["Zebra", "Apple", "Monkey", "Banana", "Car"]
    
    # First order
    source1 = "".join(f"<{c} />" for c in components)
    codeflash_output = _extract_child_components(source1, analyzer, source1); result1 = codeflash_output # 6.41μs -> 4.43μs (44.8% faster)
    
    # Reversed order
    source2 = "".join(f"<{c} />" for c in reversed(components))
    codeflash_output = _extract_child_components(source2, analyzer, source2); result2 = codeflash_output # 3.25μs -> 2.42μs (33.9% faster)

def test_component_in_jsx_expressions():
    """Test components used in JSX expressions."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<App>{condition ? <Component1 /> : <Component2 />}</App>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.53μs -> 3.95μs (40.1% faster)

def test_component_as_prop():
    """Test component passed as a prop."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Layout component={MyComponent}><Header /></Layout>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.05μs -> 3.58μs (41.2% faster)

def test_very_long_component_name():
    """Test with very long component name."""
    analyzer = TreeSitterAnalyzer("javascript")
    long_name = "VeryLongComponentNameWithManyWords" * 3
    source = f"<{long_name}><Child /></{long_name}>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.27μs -> 3.85μs (37.0% faster)

def test_unicode_component_names_not_matched():
    """Test that non-ASCII characters in component names are not matched."""
    analyzer = TreeSitterAnalyzer("javascript")
    # JSX_COMPONENT_RE uses [A-Z][a-zA-Z0-9.]* which only matches ASCII
    source = "<Component><Café /></Component>"  # é is not [a-zA-Z0-9]
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.17μs -> 3.79μs (36.5% faster)

def test_multiple_dots_in_namespace():
    """Test components with multiple dots in namespace."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<App.Components.Button><App.Components.Modal></App.Components.Modal></App.Components.Button>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.05μs -> 3.81μs (32.7% faster)

def test_regex_pattern_matches_tag_opening():
    """Test that regex correctly identifies component opening tags."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<ComponentName prop='value'>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 4.14μs -> 2.94μs (40.9% faster)

def test_closing_tags_not_double_counted():
    """Test that closing tags don't cause duplicate counting."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Component></Component>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 4.15μs -> 3.33μs (24.7% faster)

def test_self_closing_tag_matches_once():
    """Test self-closing tags are counted once."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Component /><Component /><Component />"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.31μs -> 3.71μs (43.2% faster)

def test_largest_real_world_component_tree():
    """Test with 1000 components for performance and correctness."""
    analyzer = TreeSitterAnalyzer("javascript")
    # Create components in a balanced tree structure
    unique_components = [f"Comp{i:04d}" for i in range(1000)]
    source = "".join(f"<{c}>" for c in unique_components) + "".join(f"</{c}>" for c in reversed(unique_components))
    
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 481μs -> 276μs (74.2% faster)

def test_return_is_new_list():
    """Test that function returns a new list each time (not shared state)."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Component><Child /></Component>"
    codeflash_output = _extract_child_components(source, analyzer, source); result1 = codeflash_output # 5.32μs -> 3.88μs (37.2% faster)
    codeflash_output = _extract_child_components(source, analyzer, source); result2 = codeflash_output # 2.20μs -> 1.80μs (22.2% faster)

def test_alphabetical_sort_order():
    """Test that components are sorted in strict alphabetical order."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<Z /><a /><A /><z /><M /><m />"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.38μs -> 3.75μs (43.6% faster)

def test_fragment_not_substring_match():
    """Ensure 'Fragment' filtering is exact, not substring-based."""
    analyzer = TreeSitterAnalyzer("javascript")
    source = "<FragmentProvider><Fragment><Inner /></Fragment></FragmentProvider>"
    codeflash_output = _extract_child_components(source, analyzer, source); result = codeflash_output # 5.61μs -> 4.12μs (36.3% 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-pr1563-2026-02-20T03.27.20 and push.

Codeflash Static Badge

I replaced the per-match Match object creation (finditer + group(1)) with re.findall to get the group strings directly and removed matches in bulk via set operations. This reduces object allocations and Python-level loop overhead, giving measurable speed and memory improvements on large inputs while preserving behavior and output ordering.
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Feb 20, 2026
github-actions bot and others added 2 commits February 20, 2026 03:29
Refined the optimization to focus on the core performance improvement while maximizing code simplicity:

1. **Removed module-level `_JSX_NODE_TYPES` constant**: This micro-optimization added complexity (module-level state) without meaningful performance benefit. The original tuple is small and Python handles small tuple membership checks efficiently.

2. **Removed `reversed(children)` and associated comment**: For a boolean "contains" check, the traversal order is irrelevant. Removing this simplifies the code and eliminates the overhead of reversing children lists.

3. **Kept original variable name `node`**: Reusing `node` in the loop maintains consistency with the original code and reduces diff size.

4. **Removed unnecessary comment**: The simplified iterative approach is self-explanatory and doesn't require additional documentation.

The refined code preserves the key optimization (iterative DFS avoiding recursion and generator overhead) while being more readable and closer to the original structure. The performance benefit remains intact as the core algorithmic improvement is preserved.
@codeflash-ai
Copy link
Contributor Author

codeflash-ai bot commented Feb 20, 2026

⚡️ Codeflash found optimizations for this PR

📄 457% (4.57x) speedup for _contains_jsx in codeflash/languages/javascript/frameworks/react/profiler.py

⏱️ Runtime : 621 microseconds 111 microseconds (best of 31 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch codeflash/optimize-pr1563-2026-02-20T03.27.20).

Static Badge

@claude
Copy link
Contributor

claude bot commented Feb 20, 2026

PR Review Summary

Prek Checks

Status: All passing after fixes.

Fixed 8 linting issues in commit 12d8faca:

  • TC003: Moved Path imports into TYPE_CHECKING blocks in detector.py, discovery.py, profiler.py
  • SIM110: Replaced for loops with any() in discovery.py, profiler.py
  • RET504: Removed unnecessary assignment before return in profiler.py
  • FURB171: Converted single-item tuple membership tests to equality in treesitter_utils.py

Mypy: No issues found in context.py.

Code Review

No critical issues found.

The optimization in _extract_child_components is correct and safe:

  • re.findall() with a single capture group returns group strings directly, matching the previous match.group(1) behavior
  • frozenset + difference_update is semantically equivalent to the previous per-element not in check
  • The if children: guard is a minor optimization to skip the set operation on empty results

Test Coverage

File Status Stmts Miss Cover
codeflash/languages/base.py Modified 133 2 98%
codeflash/languages/javascript/frameworks/__init__.py New 0 0 100%
codeflash/languages/javascript/frameworks/detector.py New - - 0% (not imported)
codeflash/languages/javascript/frameworks/react/__init__.py New 0 0 100%
codeflash/languages/javascript/frameworks/react/analyzer.py New - - 0% (not imported)
codeflash/languages/javascript/frameworks/react/context.py New - - 0% (not imported)
codeflash/languages/javascript/frameworks/react/discovery.py New 127 53 58%
codeflash/languages/javascript/frameworks/react/profiler.py New - - 0% (not imported)
codeflash/languages/javascript/parse.py Modified 272 138 49%
codeflash/languages/javascript/support.py Modified 1047 314 70%
codeflash/languages/javascript/treesitter_utils.py New - - 0% (not imported)
codeflash/models/function_types.py Modified 47 0 100%

Overall project coverage: 78%

Note: Several new React framework files (detector.py, analyzer.py, context.py, profiler.py, treesitter_utils.py) show 0% coverage because they are not imported during test runs. These files are part of the parent branch's React framework feature, not this optimization PR. The specific change in this PR (context.py optimization of _extract_child_components) is a mechanical refactor of the same logic.

Test failures: 8 pre-existing failures in tests/test_tracer.py unrelated to this PR.


Last updated: 2026-02-20

…2026-02-20T03.38.42

⚡️ Speed up function `_contains_jsx` by 457% in PR #1567 (`codeflash/optimize-pr1563-2026-02-20T03.27.20`)
@codeflash-ai
Copy link
Contributor Author

codeflash-ai bot commented Feb 20, 2026

This PR is now faster! 🚀 @claude[bot] accepted my optimizations from:

@claude claude bot merged commit b9834c6 into codeflash/optimize-pr1561-2026-02-20T03.15.59 Feb 20, 2026
17 of 28 checks passed
@claude claude bot deleted the codeflash/optimize-pr1563-2026-02-20T03.27.20 branch February 20, 2026 12:46
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.

0 participants

Comments