var TestString = '+'.repeat(200) + '='.repeat(20000) + '+'.repeat(200);
var TestString2 = '-'.repeat(200) + '='.repeat(20000) + '+'.repeat(200);
var TestString3 = 'a'.repeat(200) + 'b'.repeat(200) + 'c'.repeat(200) + 'd'.repeat(200) + 'e'.repeat(200) + 'f'.repeat(200) + 'g'.repeat(200) + 'h'.repeat(200) + 'i'.repeat(200) + 'j'.repeat(200) + 'k'.repeat(200) + 'l'.repeat(200);
function trimAnySet(str, chars) {
var start = 0,
end = str.length;
while(start < end && chars.has(str[start]))
++start;
while(end > start && chars.has(str[end - 1]))
--end;
return (start > 0 || end < str.length) ? str.substring(start, end) : str;
}
function trimAny(str, chars) {
var start = 0,
end = str.length;
while(start < end && chars.indexOf(str[start]) >= 0)
++start;
while(end > start && chars.indexOf(str[end - 1]) >= 0)
--end;
return (start > 0 || end < str.length) ? str.substring(start, end) : str;
}
trimAny(TestString, ['+']);
trimAnySet(TestString, new Set(['+']));
trimAny(TestString2, ['+', '-']);
trimAnySet(TestString2, new Set(['+', '-']));
trimAny(TestString3, ['a', 'b', 'c', 'd', 'e']);
trimAnySet(TestString3, new Set(['a', 'b', 'c', 'd', 'e']));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array single | |
Set single | |
Array double | |
Set double | |
Array 5 | |
Set 5 |
Test name | Executions per second |
---|---|
Array single | 132651.1 Ops/sec |
Set single | 65385.8 Ops/sec |
Array double | 126659.6 Ops/sec |
Set double | 64518.2 Ops/sec |
Array 5 | 75613.6 Ops/sec |
Set 5 | 32849.0 Ops/sec |
Measuring the performance of JavaScript trimming functions is an interesting benchmark.
What's being tested?
The provided benchmark tests the performance of two different trimming functions:
trimAny(str, chars)
: This function takes a string and an array of characters to trim from the start and end of the string.trimAnySet(str, charsSet)
: This function is similar to trimAny
, but it uses a Set
data structure instead of an array for the characters.Options being compared
The benchmark compares the performance of these two trimming functions with different input scenarios:
Pros and cons
Here's a brief overview of each function's pros and cons:
trimAny(str, chars)
: This function has a simple and straightforward approach. However, it uses the indexOf()
method to find the character in the chars
array, which may lead to slower performance compared to using a Set
.trimAnySet(str, charsSet)
: This function uses a Set
, which can provide faster lookup times than an array. However, it requires more memory and CPU cycles to create and maintain the set.Library and purpose
The two trimming functions rely on the following libraries and data structures:
indexOf()
method: A built-in JavaScript method for finding the position of a value in an array or string.Set
data structure: A built-in JavaScript data structure for storing unique values. In this benchmark, it's used to efficiently check if a character is present in the set.Special JS feature or syntax
This benchmark doesn't rely on any special JavaScript features or syntax. However, it does use some basic string manipulation techniques and array/set operations, which are widely supported across different browsers and environments.
Other alternatives
If you're interested in exploring other trimming functions or approaches, here are a few alternatives:
Overall, this benchmark provides a good starting point for comparing the performance of different trimming functions in JavaScript.