Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Trimming multiple leading/trailing characters, no set constructor overhead
(version: 0)
Comparing performance of:
Array single vs Set single vs Array double vs Set double vs Array 5 vs Set 5
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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; }
Tests:
Array single
trimAny(testString, testArray);
Set single
trimAnySet(testString, testSet);
Array double
trimAny(testString2, testArray2);
Set double
trimAnySet(testString2, testSet2);
Array 5
trimAny(testString3, testArray3);
Set 5
trimAnySet(testString3, testSet3);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Array single
Set single
Array double
Set double
Array 5
Set 5
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **Array-based trimming (`trimAny`)**: uses an array of characters to trim from the start or end of the string. 2. **Set-based trimming (`trimAnySet`)**: uses a `Set` data structure to store and look up the characters to trim. **Pros and cons** * **Array-based trimming (`trimAny`)**: + Pros: potentially faster, as arrays are often optimized for lookup and iteration. + Cons: may require more memory allocation and garbage collection due to the dynamic array. * **Set-based trimming (`trimAnySet`)**: + Pros: potentially more efficient in terms of memory usage, as `Sets` have faster lookup times. + Cons: may be slower due to the overhead of creating and managing a `Set`, especially for larger inputs. **Other considerations** * The benchmark does not consider other factors that might impact performance, such as: + String encoding (e.g., UTF-8 vs. ASCII). + Platform-specific optimizations or limitations. + Browser-specific quirks or inconsistencies. * The test strings are designed to cover various edge cases, but may not represent real-world usage scenarios. **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.
Related benchmarks:
Trimming leading/trailing characters Bounds Fix
Trimming multiple leading/trailing characters
Trimming leading/trailing characters from string
Trimming leading/trailing characters again
Comments
Confirm delete:
Do you really want to delete benchmark?