Merged
Conversation
JS projects often have package.json main/module/exports pointing to build output directories (build/, dist/, out/) which contain compiled code. Now the detector prioritizes common source directories (src/, lib/, source/) and skips build output paths when determining module root.
The js-suggester branch captures config_file from has_existing_config() to display the path in --show-config output.
The detect_module_root() in config_js.py was not updated when detector.py's _detect_js_module_root() was fixed to skip build output directories. This caused --show-config to display 'src' but --all to use 'build/src'. Changes: - Add is_build_output_dir() helper function - Prioritize src/, lib/, source/ directories first - Skip build output dirs (build/, dist/, out/, .next/, .nuxt/) when extracting paths from package.json fields
Import from codeflash.setup.detector instead of defining locally.
The optimized code achieves a **143% speedup** (from 503μs to 206μs) through three key improvements:
## Primary Optimizations
1. **Eliminated expensive string operations**: The original code called `path.as_posix().split("/")` on every invocation, which:
- Converts the Path object to a POSIX string representation
- Allocates a new string
- Splits it into a list
The optimized version uses `path.parts`, which is a cached tuple property already available on Path objects, eliminating ~61% of the original runtime (line profiler shows `as_posix().split()` took 60.9% of execution time).
2. **Avoided set reconstruction**: The original code recreated the `build_dirs` set on every function call. The optimized version pre-computes `_BUILD_DIRS` as a module-level `frozenset`, eliminating the 5.5% overhead from set creation.
3. **Leveraged efficient set operation**: Instead of using a generator expression with `any()` that performs multiple `in` checks (33.6% of original time), the optimized code uses `isdisjoint()` - a built-in set method implemented in C that performs a single, optimized intersection check.
## Why This Matters
The test results show consistent speedups across all scenarios:
- **Simple paths**: 2-3x faster (e.g., `Path("build")`: 5.19μs → 2.34μs)
- **Deep nesting**: 2-3x faster even with complex paths
- **Large-scale tests**: 3-4x faster for paths with 100+ components (e.g., 100 regular dirs: 14.8μs → 4.26μs, **248% speedup**)
- **Worst-case scenarios**: The optimization shines brightest when checking long paths with no matches (21.7μs → 4.78μs, **355% speedup**)
The optimization scales particularly well because `isdisjoint()` can short-circuit as soon as it finds a match, while `path.parts` access is essentially free after the first call. This makes the function highly efficient for both hot paths that check many paths repeatedly and deep directory structures common in real projects.
Contributor
⚡️ Codeflash found optimizations for this PR📄 144% (1.44x) speedup for
|
Tests now correctly expect '.' when package.json exports points to build output directories (dist/, build/, etc.) without a src/ directory.
…2026-02-03T12.05.29 ⚡️ Speed up function `is_build_output_dir` by 144% in PR #1308 (`js-suggester`)
Use starting_line/ending_line/starting_col/ending_col instead of start_line/end_line/start_col/end_col in test_with_tricky_helpers test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.