⚡️ Speed up method ImportResolver._is_external_package by 42% in PR #1266 (alias_support_in_ts) - #1291
Closed
codeflash-ai[bot] wants to merge 1 commit into
Closed
Conversation
The optimization achieves a **41% runtime improvement** (539μs → 379μs) by consolidating four sequential `startswith()` checks into a single tuple-based check.
**Key optimization:** Python's `str.startswith()` method accepts a tuple of prefixes and performs the check in optimized C code, scanning the string once rather than four times. The original code made four separate method calls:
```python
if module_path.startswith("."):
return False
if module_path.startswith("/"):
return False
if module_path.startswith("@/"):
return False
if module_path.startswith("~/"):
return False
```
The optimized version combines these into one check:
```python
if module_path.startswith((".", "/", "@/", "~/")):
return False
```
**Why this is faster:**
1. **Single method call overhead:** Reduces four method invocations to one
2. **Single string scan:** The internal C implementation scans the string once to test all prefixes, rather than potentially scanning up to four times
3. **Short-circuit evaluation:** Once any prefix matches, the check completes immediately
**Line profiler evidence:** The original code spent 1,745,286ns across multiple checks (30.5% + 23.7% + 16.4% + 12.4% = 83% of total time). The optimized version completes the same logic in 709,412ns (64.6% of total time), representing a ~60% reduction in the critical path.
**Test case analysis:** The optimization excels across all scenarios:
- **Alias patterns** (`@/` and `~/`): 28-73% faster since these previously required checking three prefixes before matching
- **Bare imports** (external packages): 40-83% faster as they now fail all four prefix checks in a single operation
- **Large-scale batches**: 42-73% faster for mixed workloads and scoped packages
The optimization maintains identical behavior and correctness while delivering consistent performance gains across all import types commonly found in JavaScript/TypeScript projects.
This was referenced Feb 17, 2026
Contributor
|
Closing stale bot PR. |
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.
⚡️ This pull request contains optimizations for PR #1266
If you approve this dependent PR, these changes will be merged into the original PR branch
alias_support_in_ts.📄 42% (0.42x) speedup for
ImportResolver._is_external_packageincodeflash/languages/javascript/import_resolver.py⏱️ Runtime :
539 microseconds→379 microseconds(best of194runs)📝 Explanation and details
The optimization achieves a 41% runtime improvement (539μs → 379μs) by consolidating four sequential
startswith()checks into a single tuple-based check.Key optimization: Python's
str.startswith()method accepts a tuple of prefixes and performs the check in optimized C code, scanning the string once rather than four times. The original code made four separate method calls:The optimized version combines these into one check:
Why this is faster:
Line profiler evidence: The original code spent 1,745,286ns across multiple checks (30.5% + 23.7% + 16.4% + 12.4% = 83% of total time). The optimized version completes the same logic in 709,412ns (64.6% of total time), representing a ~60% reduction in the critical path.
Test case analysis: The optimization excels across all scenarios:
@/and~/): 28-73% faster since these previously required checking three prefixes before matchingThe optimization maintains identical behavior and correctness while delivering consistent performance gains across all import types commonly found in JavaScript/TypeScript projects.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1266-2026-02-03T06.49.57and push.