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 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;
}
function splitTrim(str, ch) {
let res = str.split(ch);
return res.length > 1 ? res[1] : res[0];
}
indexTrim(TestString, '+');
regexTrim(TestString, '+')
booleanTrim(TestString, '+')
spreadTrim(TestString, '+')
substringTrim(TestString, '+')
splitTrim(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) | |
Split Version (CheapSk8) |
Test name | Executions per second |
---|---|
Index Version (Jason Larke) | 991832.6 Ops/sec |
Regex Version (leaf) | 131508.1 Ops/sec |
Boolean Filter Version (mbaer3000) | 155215.6 Ops/sec |
Spread Version (Robin F.) | 971.6 Ops/sec |
Substring Version (Pho3niX83) | 237048.7 Ops/sec |
Split Version (CheapSk8) | 262914.6 Ops/sec |
I'd be happy to help explain the benchmark and its results.
Benchmark Overview
The benchmark measures how fast different string trimming functions are executed in JavaScript on a desktop machine running Chrome 120. The test case uses a predefined string TestString
that contains a mix of characters, including the character +
, which is used as the trimming character in all test cases.
Test Cases and Trimming Functions
There are six test cases:
+
character remains.Results
The benchmark results show the number of executions per second for each test case on a Chrome 120 browser running on a Windows desktop machine. The top-performing test cases are:
Pros and Cons of Each Approach
Here's a brief summary of the pros and cons of each approach:
Conclusion
In summary, the index version is the clear winner in this benchmark, followed closely by the substring version. The regex and spread versions are relatively slower due to their overhead, while the boolean filter version is slow but can be effective for short strings. The split version is the slowest due to its unnecessary splitting operation.
It's worth noting that these results may vary depending on the specific use case and input data.