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,
chList = [ch];
while(start < end && chList.includes(str[start]))
++start;
while(end > start && chList.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) | 167225.4 Ops/sec |
Regex Version (leaf) | 75542.3 Ops/sec |
Boolean Filter Version (mbaer3000) | 44754.6 Ops/sec |
Spread Version (Robin F.) | 2925.8 Ops/sec |
Substring Version (Pho3niX83) | 145031.4 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance of four different trimming algorithms for leading and trailing characters in a string:
indexTrim
: Uses a simple iterative approach to find and remove leading/trailing characters.regexTrim
: Utilizes regular expressions to match and replace leading/trailing characters.booleanTrim
: Employs the split()
method followed by filtering out empty strings using Boolean
.spreadTrim
: Uses the spread operator (...
) to find the first and last occurrence of a character, then removes them from the string.Options Compared
The benchmark compares the performance of each algorithm across various browsers (Chrome 92) on different devices (Desktop).
Pros and Cons of Each Approach
indexTrim
:regexTrim
:booleanTrim
:split()
method and filtering out empty strings.spreadTrim
:Library Used
None of the provided code snippets use external libraries. However, some browsers may have built-in optimizations or features that could affect the performance of these algorithms (e.g., Chrome's String.prototype.trim()
method uses regular expressions under the hood).
Special JS Features/Syntax
The benchmark does not utilize any special JavaScript features or syntax beyond what is standard in ECMAScript 2020.
Alternatives
Other trimming algorithms or approaches could be explored, such as:
String.prototype.replaceAll()
with a replacement string of an empty character.trim
function.Keep in mind that these alternatives may have different performance characteristics, trade-offs, or even be less suitable for certain use cases.