Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Trimming leading/trailing characterss
(version: 0)
Comparing performance of:
Index Version (Jason Larke) vs Regex Version (leaf) vs Boolean Filter Version (mbaer3000) vs Spread Version (Robin F.) vs Substring Version (Pho3niX83)
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var TestString = '+'.repeat(200) + '='.repeat(20000) + '+'.repeat(200); 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, chList = [...ch]; while(start < end && chList.includes(str[start])) ++start; while(end > start && chList.includes(str[end - 1])) --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; }
Tests:
Index Version (Jason Larke)
indexTrim(TestString, '+');
Regex Version (leaf)
regexTrim(TestString, '+')
Boolean Filter Version (mbaer3000)
booleanTrim(TestString, '+')
Spread Version (Robin F.)
spreadTrim(TestString, '+')
Substring Version (Pho3niX83)
substringTrim(TestString, '+')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Index Version (Jason Larke)
Regex Version (leaf)
Boolean Filter Version (mbaer3000)
Spread Version (Robin F.)
Substring Version (Pho3niX83)
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):
**Benchmark Overview** The provided benchmark measures the performance of four different trimming algorithms for leading and trailing characters in a string: 1. `indexTrim`: Uses a simple iterative approach to find and remove leading/trailing characters. 2. `regexTrim`: Utilizes regular expressions to match and replace leading/trailing characters. 3. `booleanTrim`: Employs the `split()` method followed by filtering out empty strings using `Boolean`. 4. `spreadTrim`: Uses the spread operator (`...`) to find the first and last occurrence of a character, then removes them from the string. **Options Compared** The benchmark compares the performance of each algorithm across various browsers (Chrome 92) on different devices (Desktop). **Pros and Cons of Each Approach** 1. `indexTrim`: * Pros: Simple, easy to understand, and potentially efficient for small strings. * Cons: May not be optimized for larger strings or edge cases. 2. `regexTrim`: * Pros: Flexible and effective for matching specific patterns, but can be slower due to the overhead of regular expressions. * Cons: May have performance issues with complex patterns or very large strings. 3. `booleanTrim`: * Pros: Simple and efficient, as it leverages the `split()` method and filtering out empty strings. * Cons: May not be suitable for strings with only one character to trim. 4. `spreadTrim`: * Pros: Potentially efficient for large strings, as it avoids iterating over the entire string. * Cons: May have performance issues if the first or last occurrence of a character is rare. **Library Used** None of the provided code snippets use external libraries. However, some browsers may have built-in optimizations or features that could affect the performance of these algorithms (e.g., Chrome's `String.prototype.trim()` method uses regular expressions under the hood). **Special JS Features/Syntax** The benchmark does not utilize any special JavaScript features or syntax beyond what is standard in ECMAScript 2020. **Alternatives** Other trimming algorithms or approaches could be explored, such as: 1. Using `String.prototype.replaceAll()` with a replacement string of an empty character. 2. Leveraging a library like Lodash's `trim` function. 3. Implementing a custom trimming algorithm using bitwise operations (e.g., checking the first and last characters of the string). Keep in mind that these alternatives may have different performance characteristics, trade-offs, or even be less suitable for certain use cases.
Related benchmarks:
Trimming leading/trailing characters Bounds
Trimming leading/trailing characters Bounds Fix
Trimming leading/trailing characters Bounds Fix2
Trimming leading/trailing characters again
Comments
Confirm delete:
Do you really want to delete benchmark?