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 && ch.includes(str[start]))
++start;
while(end > start && ch.includes(str[end - 1]))
--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, '+')
--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) |
Test name | Executions per second |
---|---|
Index Version (Jason Larke) | 77887.4 Ops/sec |
Regex Version (leaf) | 87319.9 Ops/sec |
Boolean Filter Version (mbaer3000) | 45089.8 Ops/sec |
Spread Version (Robin F.) | 801.5 Ops/sec |
Substring Version (Pho3niX83) | 252893.7 Ops/sec |
This is a JavaScript benchmarking test, and we're going to break it down step by step.
Benchmark Definition
The benchmark defines four different trimming functions:
indexTrim(str, ch)
: Removes leading and trailing characters from the string using an index-based approach.regexTrim(str, ch)
: Removes leading and trailing characters from the string using a regular expression.booleanTrim(str, ch)
: Removes leading and trailing characters from the string using boolean filtering (splitting the string into substrings around the character).spreadTrim(str, ch)
: Removes leading and trailing characters from the string using a spread-based approach.Library
The benchmark uses two libraries:
RegExp
(regular expressions): used in regexTrim
.Array.prototype.findIndex
, Array.prototype.reverse
, Array.prototype.push
, and String.prototype.includes
( Array methods): used in indexTrim
and spreadTrim
.Options being compared
The benchmark compares the performance of each trimming function across different browsers (Firefox 111) on various devices.
Pros and Cons
Here's a brief overview of each approach:
indexTrim
)regexTrim
)indexTrim
due to the overhead of regular expressions.booleanTrim
)spreadTrim
)Other considerations
TestString
to evaluate the performance of each trimming function. This may not accurately represent real-world usage scenarios.Alternatives
Other approaches to trimming strings in JavaScript include:
String.prototype.replace()
with a callback function.Keep in mind that these alternatives may have different performance characteristics and trade-offs, depending on the specific use case and requirements.