var TestString = '+'.repeat(200) + '='.repeat(20000) + '+'.repeat(200);
var regexTrim = function(str, ch) {
if (ch === ']') ch = '\\]';
else if (ch === '^') ch = '\\^';
else if (ch === '\\') ch = '\\\\';
return str.replace(new RegExp('^[' + ch + ']+|[' + ch + ']+$', 'g'), '');
};
function indexTrim(str, ch) {
var start = 0,
end = str.length;
while(start < end && str[start] === ch)
++start;
while(end > start && str[end - 1] === ch)
--end;
return (start > 0 || end < str.length) ? str.substring(start, end) : str;
}
function indexTrimB(str, ch) {
var start = 0,
end = str.length - 1;
while(start <= end && str[start] === ch)
++start;
while(end >= start && str[end] === ch)
--end;
return (start > 0 || end <= str.length) ? str.substring(start, end) : str;
}
function booleanTrim(str, ch) {
return str.split(ch).filter(Boolean).join(ch);
}
function spreadTrim(str, ch) {
const first = [str].findIndex(char => char !== ch);
const last = [str].reverse().findIndex(char => char !== ch);
return str.substring(first, str.length - last);
}
function substringTrim(str, ch) {
while(str.charAt(0)==ch) {
str = str.substring(1);
}
while(str.charAt(str.length-1)==ch) {
str = str.substring(0,str.length-1);
}
return str;
}
indexTrim(TestString, '+');
regexTrim(TestString, '+')
booleanTrim(TestString, '+')
spreadTrim(TestString, '+')
substringTrim(TestString, '+')
indexTrimB(TestString, '+');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Index Version (Jason Larke) | |
Regex Version (leaf) | |
Boolean Filter Version (mbaer3000) | |
Spread Version (Robin F.) | |
Substring Version (Pho3niX83) | |
Index Version (Jason Larke) B |
Test name | Executions per second |
---|---|
Index Version (Jason Larke) | 587548.1 Ops/sec |
Regex Version (leaf) | 75710.3 Ops/sec |
Boolean Filter Version (mbaer3000) | 89191.0 Ops/sec |
Spread Version (Robin F.) | 3754.4 Ops/sec |
Substring Version (Pho3niX83) | 233431.5 Ops/sec |
Index Version (Jason Larke) B | 618211.9 Ops/sec |
Measuring performance of different string trimming approaches in JavaScript can be an interesting task.
The provided JSON benchmark definition represents the test cases that are being compared. Each test case uses a different approach to trim a string:
indexTrim(str, ch)
: This function trims leading and trailing characters from the input string using two while loops. It's a simple and straightforward implementation.regexTrim(TestString, '+')
: This function uses regular expressions to match and remove leading and trailing '+' characters from the input string. The $
character in the regex pattern ensures that matching only occurs at the start and end of the string.booleanTrim(TestString, '+')
: This function splits the input string into an array using '+' as a separator, filters out empty strings, and then joins them back together with '+' in between.spreadTrim(TestString, '+')
: This function uses the spread operator (...
) to create an array of characters from the input string, finds the indices of the first and last occurrence of '+' using findIndex()
, and then returns a substring from that index to the end (or beginning) of the original string.substringTrim(TestString, '+')
: This function uses the charAt()
method to remove leading and trailing '+' characters by continuously calling substring()
with increasing or decreasing indices until no more '+' characters are found.Now, let's discuss the pros and cons of each approach:
Performance
indexTrim(str, ch)
: Simple and efficient, but may not be as effective for very large strings.regexTrim(TestString, '+')
: May be slower due to regular expression matching, especially for very large strings.booleanTrim(TestString, '+')
: Less efficient due to the overhead of string splitting and filtering.spreadTrim(TestString, '+')
: May be faster due to using array operations, but can be less intuitive for developers without experience with modern JavaScript features.substringTrim(TestString, '+')
: Simple and efficient, but may not handle edge cases correctly.Readability and Maintainability
indexTrim(str, ch)
: Easy to understand and maintain, especially for developers familiar with basic string manipulation.regexTrim(TestString, '+')
: May be harder to read due to the use of regular expressions.booleanTrim(TestString, '+')
: Less readable due to the use of array operations and string splitting.spreadTrim(TestString, '+')
: May require more experience with modern JavaScript features for developers to understand.substringTrim(TestString, '+')
: Easy to read and maintain, but may not handle edge cases correctly.Browser Support
All of these approaches should work in most modern browsers that support JavaScript. However, the performance differences may become more pronounced on older or lower-end devices.
The benchmark results show that:
indexTrim(str, ch)
outperforms the other approaches for strings with a single '+' character.substringTrim(TestString, '+')
provides the best overall performance for trimming leading and trailing '+' characters from large strings.spreadTrim(TestString, '+')
is slower than substringTrim(TestString, '+')
but still competitive.Overall, the choice of string trimming approach depends on the specific requirements of your project, including performance, readability, maintainability, and browser support.