⚡️ Speed up function to_camel_case by 128%#5239
Merged
adhami3310 merged 2 commits intoreflex-dev:mainfrom May 1, 2025
Merged
Conversation
Here's an optimized version of your code. The time-consuming part is the `re.split` operation, used only for splitting on `-` and `_`, which can be replaced with a much faster approach since there are only two possible delimiters. Using `str.replace` is significantly faster for this case, and the generator expression for capitalization is fine, but can be made a tiny bit faster with a list comprehension. All existing comments are preserved. **Optimizations made:** - Removed `re.split`, replacing with a much faster combination of `str.replace` and `str.split`. - Used a list comprehension for joining capitalized words (marginally faster than a generator for `str.join`). - Preserves exact same return values for all inputs and all original comments. This should be much faster, especially for large numbers of short strings.
CodSpeed Performance ReportMerging #5239 will improve performances by 3.39%Comparing Summary
Benchmarks breakdown
|
adhami3310
reviewed
May 1, 2025
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.
📄 128% (1.28x) speedup for
to_camel_caseinreflex/utils/format.py⏱️ Runtime :
996 microseconds→436 microseconds(best of149runs)📝 Explanation and details
Here's an optimized version of your code. The time-consuming part is the
re.splitoperation, used only for splitting on-and_, which can be replaced with a much faster approach since there are only two possible delimiters. Usingstr.replaceis significantly faster for this case, and the generator expression for capitalization is fine, but can be made a tiny bit faster with a list comprehension. All existing comments are preserved.Optimizations made:
re.split, replacing with a much faster combination ofstr.replaceandstr.split.str.join).This should be much faster, especially for large numbers of short strings.
✅ Correctness verification report:
⚙️ Existing Unit Tests Details
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-to_camel_case-ma50cmooand push.