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.indexOf(str[start])>-1)
++start;
while(end > start && ch.indexOf(str[end - 1])>-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) | 65627.2 Ops/sec |
Regex Version (leaf) | 75848.2 Ops/sec |
Boolean Filter Version (mbaer3000) | 44504.8 Ops/sec |
Spread Version (Robin F.) | 2907.5 Ops/sec |
Substring Version (Pho3niX83) | 142670.7 Ops/sec |
Let's break down the provided benchmark definitions and explain what is being tested.
Benchmark Definition JSON
The JSON represents a benchmark definition with several scripts, including indexTrim
, regexTrim
, booleanTrim
, spreadTrim
, and substringTrim
. Each script takes two arguments: str
(the input string) and ch
(the character to trim).
indexTrim(str, ch)
:ch
.ch
removed.regexTrim(str, ch)
:ch
at the start or end of the string.]
, ^
, and \\
characters is done by escaping them if necessary.booleanTrim(str, ch)
:ch
as a separator.Boolean
.ch
.spreadTrim(str, ch)
:ch
in the string using spread syntax.ch
removed between these indices.substringTrim(str, ch)
:ch
from the input string.Comparison of approaches
The benchmark tests different approaches to trimming characters from a string:
indexTrim
regexTrim
booleanTrim
spreadTrim
substringTrim
Library usage
The benchmark uses several libraries:
Array.prototype.findIndex
, String.prototype.includes
).Special JS feature or syntax
The benchmark does not appear to use any special JavaScript features or syntax, such as async/await, arrow functions, or generators.
Alternatives
Other alternatives for trimming characters from a string include:
Array.prototype.slice()
to create a new array with trimmed elements.String.prototype.replace()
method with a regex pattern and replacement function.Keep in mind that each approach has its trade-offs, and the best choice depends on the specific requirements of your application.