From 80d0f534e04c27fbe0addea1be83e24a52202a64 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:34:53 +0000 Subject: [PATCH] Optimize _contains_jsx Using an explicit stack (iterative DFS) avoids Python recursion overhead and the temporary generator created by any(...). This reduces function-call overhead on large/deep trees and returns as soon as a matching node is found, improving both runtime and memory usage. --- .../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..1321d0af5 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: + current = stack.pop() + if current.type in ("jsx_element", "jsx_self_closing_element", "jsx_fragment"): + return True + stack.extend(current.children) + return False def _wrap_return_with_profiler(source: str, return_node: Node, profiler_id: str, safe_name: str) -> str: