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 trimSuffix(str, suffix) {
let lastStrChar = str.length - 1, lastSuffixChar = suffix.length - 1;
while (lastSuffixChar >= 0) {
if (str[lastStrChar] !== suffix[lastSuffixChar]) return str;
lastStrChar--;
lastSuffixChar--;
}
return str.slice(0, lastStrChar + 1)
}
indexTrim(TestString, '+');
regexTrim(TestString, '+')
booleanTrim(TestString, '+')
spreadTrim(TestString, '+')
substringTrim(TestString, '+')
trimSuffix(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 Multiple Char Version (Pawel O.) |
Test name | Executions per second |
---|---|
Index Version (Jason Larke) | 1343272.6 Ops/sec |
Regex Version (leaf) | 161773.9 Ops/sec |
Boolean Filter Version (mbaer3000) | 187392.5 Ops/sec |
Spread Version (Robin F.) | 8524.0 Ops/sec |
Substring Version (Pho3niX83) | 425688.6 Ops/sec |
Index Multiple Char Version (Pawel O.) | 103549560.0 Ops/sec |
The benchmark in question evaluates various approaches to trim leading and trailing characters from a string in JavaScript. The string used for testing, TestString
, consists of repeated '+' and '=' characters, making it particularly suited for examining how each method handles these leading and trailing characters.
Index Version (indexTrim
):
Regex Version (regexTrim
):
Boolean Filter Version (booleanTrim
):
Spread Version (spreadTrim
):
Substring Version (substringTrim
):
Trim Suffix Version (trimSuffix
):
From the benchmark results, the predominant winner was the Index Trim Version, achieving over 103 million executions per second, indicating a very efficient implementation for the specific input type. The Regex Version, alongside Substring and Boolean Filter Versions, performed significantly slower, highlighting the efficacy of more straightforward index-based approaches for this type of problem input.
Beyond the discussed methods, additional strategies could include:
String.prototype.trim
(not effective for specific characters but useful for whitespace).In summary, the benchmark provides a perspective on different implementations for trimming operations in strings, weighing their performance, complexities, and suitable use cases.