Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Trimming multiple leading/trailing characters
(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); 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, ['+']);
Set single
trimAnySet(TestString, new Set(['+']));
Array double
trimAny(TestString2, ['+', '-']);
Set double
trimAnySet(TestString2, new Set(['+', '-']));
Array 5
trimAny(TestString3, ['a', 'b', 'c', 'd', 'e']);
Set 5
trimAnySet(TestString3, new Set(['a', 'b', 'c', 'd', 'e']));
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):
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: 1. `trimAny(str, chars)`: This function takes a string and an array of characters to trim from the start and end of the string. 2. `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: * Single character trimming: Testing the performance of both functions when only one character is used for trimming. * Double character trimming: Testing the performance of both functions when two characters are used for trimming. * Trimming a large set of characters: Testing the performance of both functions when a large array or set of characters is used for trimming. **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: * Using regular expressions (regex) to trim strings: This approach can be more efficient than using arrays or sets, but it may also lead to slower performance for very large inputs. * Implementing a custom trimming algorithm: Depending on the specific requirements and constraints of your use case, you might want to explore implementing a custom trimming algorithm that's optimized for your specific needs. Overall, this benchmark provides a good starting point for comparing the performance of different trimming functions in JavaScript.
Related benchmarks:
Trimming leading/trailing characterss
Trimming multiple leading/trailing characters, no set constructor overhead
Trimming leading/trailing characters from string
Trimming leading/trailing characters again
Comments
Confirm delete:
Do you really want to delete benchmark?