var TestString = '+'.repeat(2) + '='.repeat(20) + '+'.repeat(1);
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 + 1 < str.length ? str.substring(start, end + 1) : 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) | 5892355.5 Ops/sec |
Regex Version (leaf) | 1718431.1 Ops/sec |
Boolean Filter Version (mbaer3000) | 1436345.2 Ops/sec |
Spread Version (Robin F.) | 1809522.8 Ops/sec |
Substring Version (Pho3niX83) | 5123789.0 Ops/sec |
Index Version (Jason Larke) B | 5859019.0 Ops/sec |
I'll break down the benchmark and its test cases to explain what's being tested.
Benchmark Definition
The benchmark definition is a JSON object that describes how to run a JavaScript script on MeasureThat.net. It contains three main parts:
regexTrim
that takes two arguments: str
and ch
. The function uses a regular expression to trim the leading and trailing characters of the input string str
, specified by character ch
.Individual Test Cases
There are six test cases, each representing a different implementation of string trimming:
ch
from the input string str
.str
, specified by character ch
.split()
method, filtering out empty strings created when removing leading or trailing occurrences of ch
.ch
from the start of the string, and another to find the index of the last occurrence of ch
from the end of the string. Then it uses spread operator to trim.ch
from the input string str
, which modifies the original string.Test Results
The benchmark results show the number of executions per second for each test case on a specific browser and device platform (Chrome 91 on Windows).
Key Observations
Overall, this benchmark highlights the differences in implementation details and their impact on performance.