var TestString = '+'.repeat(1) + '='.repeat(20000) + '+'.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 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) | 11997127.0 Ops/sec |
Regex Version (leaf) | 150161.0 Ops/sec |
Boolean Filter Version (mbaer3000) | 1363966.0 Ops/sec |
Spread Version (Robin F.) | 4151.9 Ops/sec |
Substring Version (Pho3niX83) | 10903854.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
The provided JSON represents a benchmark test case that compares different approaches for trimming leading/trailing characters from a string. The main goal is to determine which method is the most efficient.
Benchmark Definition
The Benchmark Definition
json object defines the base benchmark, which includes:
Options Compared
The benchmark compares six different approaches for trimming leading/trailing characters:
Pros and Cons
Each approach has its strengths and weaknesses:
Library Usage
None of the provided functions explicitly use any external libraries. However, some approaches (e.g., regexTrim) rely on built-in JavaScript features, while others (e.g., indexTrim and substringTrim) are more low-level and do not utilize any external libraries.
Special JS Feature/Syntax
The benchmark does not specifically test or utilize any special JavaScript features or syntax beyond the standard language. However, it's worth noting that some implementations might be sensitive to specific browser or engine optimizations (e.g., WebAssembly or Just-In-Time compilation).
Alternatives
If you're interested in exploring other approaches for trimming leading/trailing characters, consider the following alternatives:
The benchmark test case provides a solid foundation for comparing different approaches, but feel free to experiment with new ideas and algorithms to optimize your own trimming tasks!