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 indexTrimB(str, ch) {
var start = 0,
end = str.length - 1;
while(start <= end && str[start] === ch)
++start;
while(end >= start && str[end] === ch)
--end;
return (start > 0 || end < str.length - 1) ? 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, '+')
indexTrimB(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 Version (Jason Larke) B |
Test name | Executions per second |
---|---|
Index Version (Jason Larke) | 551615.7 Ops/sec |
Regex Version (leaf) | 73931.5 Ops/sec |
Boolean Filter Version (mbaer3000) | 91273.3 Ops/sec |
Spread Version (Robin F.) | 3871.1 Ops/sec |
Substring Version (Pho3niX83) | 234902.2 Ops/sec |
Index Version (Jason Larke) B | 658822.9 Ops/sec |
I'll break down the benchmark definition and explain what's being tested, along with the pros and cons of each approach.
Benchmark Definition:
The benchmark tests four different implementations of string trimming:
indexTrim(str, ch)
regexTrim(str, ch)
booleanTrim(str, ch)
spreadTrim(str, ch)
substringTrim(str, ch)
These functions aim to remove leading and trailing occurrences of a specified character ch
from a string str
.
Options Compared:
The benchmark compares the performance of each implementation across different browsers, devices, and operating systems.
Pros and Cons of Each Approach:
findIndex
and substring
methods.Library Used:
The booleanTrim
function uses the split
, filter
, and join
methods from JavaScript's built-in String API. These methods are part of the ECMAScript Standard, so they're widely supported across browsers.
Special JS Feature or Syntax:
None of the functions in this benchmark utilize any special JavaScript features or syntax beyond standard ECMAScript functionality.
Other Considerations:
TestString
) to simulate real-world scenarios and ensure that each implementation is thoroughly tested.In conclusion, this benchmark provides a comprehensive comparison of different string trimming implementations in JavaScript, highlighting their strengths and weaknesses. By analyzing the results, developers can choose the most suitable approach for their specific use case, taking into account factors like performance, simplicity, and edge case handling.