@@ -230,6 +230,12 @@ def discover_tests(
230230 """
231231 result : dict [str , list [TestInfo ]] = {}
232232
233+ # Build index: function_name → qualified_name for O(1) lookup
234+ # This avoids iterating all functions for every test file (was O(NxM), now O(N+M))
235+ function_name_to_qualified : dict [str , str ] = {}
236+ for func in source_functions :
237+ function_name_to_qualified [func .function_name ] = func .qualified_name
238+
233239 # Find all test files using language-specific patterns
234240 test_patterns = self ._get_test_patterns ()
235241
@@ -254,13 +260,15 @@ def discover_tests(
254260 # Find test functions (describe/it/test blocks)
255261 test_functions = self ._find_jest_tests (source , analyzer )
256262
257- # Match source functions to tests
258- for func in source_functions :
259- if func .function_name in imported_names or func .function_name in source :
260- if func .qualified_name not in result :
261- result [func .qualified_name ] = []
263+ # Match source functions to tests using the index
264+ # Only check functions that are actually imported in this test file
265+ for imported_name in imported_names :
266+ if imported_name in function_name_to_qualified :
267+ qualified_name = function_name_to_qualified [imported_name ]
268+ if qualified_name not in result :
269+ result [qualified_name ] = []
262270 for test_name in test_functions :
263- result [func . qualified_name ].append (
271+ result [qualified_name ].append (
264272 TestInfo (test_name = test_name , test_file = test_file , test_class = None )
265273 )
266274 except Exception as e :
0 commit comments