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);
var testArray = ['+'];
var testSet = new Set(testArray);
var testArray2 = ['+', '-'];
var testSet2 = new Set(testArray2);
var testArray3 = ['a', 'b', 'c', 'd', 'e'];
var testSet3 = new Set(testArray3);
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, testArray);
trimAnySet(testString, testSet);
trimAny(testString2, testArray2);
trimAnySet(testString2, testSet2);
trimAny(testString3, testArray3);
trimAnySet(testString3, testSet3);
--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 | 267136.5 Ops/sec |
Set single | 199973.2 Ops/sec |
Array double | 234984.5 Ops/sec |
Set double | 170225.2 Ops/sec |
Array 5 | 78295.0 Ops/sec |
Set 5 | 68348.0 Ops/sec |
Let's dive into the benchmark.
The provided benchmark measures the performance of two string trimming functions: trimAny
and trimAnySet
, in different scenarios with various data structures (arrays and sets).
What is tested?
The benchmark tests the execution speed of the trimAny
and trimAnySet
functions on a set of predefined test strings. The test strings are generated by repeating specific characters ('+', '-', 'a' to 'g') in varying lengths.
The functions being tested:
trimAny
: takes two arguments, str
(the input string) and chars
(an array of characters to trim from the start or end of the string).trimAnySet
: similar to trimAny
, but uses a Set
data structure instead of an array for chars
.Options compared
The benchmark compares two approaches:
trimAny
): uses an array of characters to trim from the start or end of the string.trimAnySet
): uses a Set
data structure to store and look up the characters to trim.Pros and cons
trimAny
):trimAnySet
):Sets
have faster lookup times.Set
, especially for larger inputs.Other considerations
Library and features
No specific JavaScript library is mentioned in the benchmark definition or execution code. However, the use of Set
data structures suggests that modern JavaScript engines (e.g., V8) have optimized support for this data structure.
Special JS feature or syntax
The benchmark does not explicitly test any special JavaScript features or syntax.