From 28d6943846a4ed49a09176cba4299c7810147251 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Fri, 20 Feb 2026 03:38:46 +0000 Subject: [PATCH] Optimize _contains_jsx 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. --- .../languages/javascript/frameworks/react/profiler.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/codeflash/languages/javascript/frameworks/react/profiler.py b/codeflash/languages/javascript/frameworks/react/profiler.py index f817c375d..e95d6f341 100644 --- a/codeflash/languages/javascript/frameworks/react/profiler.py +++ b/codeflash/languages/javascript/frameworks/react/profiler.py @@ -160,9 +160,13 @@ def walk(node: Node) -> None: def _contains_jsx(node: Node) -> bool: """Check if a tree-sitter node contains JSX elements.""" - if node.type in ("jsx_element", "jsx_self_closing_element", "jsx_fragment"): - return True - return any(_contains_jsx(child) for child in node.children) + stack = [node] + while stack: + node = stack.pop() + if node.type in ("jsx_element", "jsx_self_closing_element", "jsx_fragment"): + return True + stack.extend(node.children) + return False def _wrap_return_with_profiler(source: str, return_node: Node, profiler_id: str, safe_name: str) -> str: