⚡️ Speed up method ImportResolver._is_external_package by 42% in PR #1266 (alias_support_in_ts)
#1291
+1
−10
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.