This is a micro optimization to make String/removingLeadingWhitespace() and String/removingTrailingWhitespace() faster. This difference is only measurable in micro benchmarks.
I stumbled upon this code while reviewing something else and the repeated String copying irked me.
Using micro benchmarks I could confirm that using firstIndex(where:)/ lastIndex(where:) is faster for all amounts of leading and trailing whitespace. In the graphs below, all measurements are divided by the string length.
Bug/issue #, if applicable:
Summary
This is a micro optimization to make
String/removingLeadingWhitespace()
andString/removingTrailingWhitespace()
faster. This difference is only measurable in micro benchmarks.I stumbled upon this code while reviewing something else and the repeated String copying irked me.
Using micro benchmarks I could confirm that using
firstIndex(where:)
/lastIndex(where:)
is faster for all amounts of leading and trailing whitespace. In the graphs below, all measurements are divided by the string length.1 leading and 1 trailing space
n leading and n trailing spaces
0 leading and 0 trailing spaces
Note that green compares to red and yellow compares to blue.
Dependencies
None
Testing
None. This isn't a change in behavior.
Checklist
Make sure you check off the following items. If they cannot be completed, provide a reason.
./bin/test
script and it succeeded