Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Trimming leading/trailing characterssss
(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; while(start < end && ch.includes(str[start])) ++start; while(end > start && ch.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):
This is a JavaScript benchmarking test, and we're going to break it down step by step. **Benchmark Definition** The benchmark defines four different trimming functions: 1. `indexTrim(str, ch)`: Removes leading and trailing characters from the string using an index-based approach. 2. `regexTrim(str, ch)`: Removes leading and trailing characters from the string using a regular expression. 3. `booleanTrim(str, ch)`: Removes leading and trailing characters from the string using boolean filtering (splitting the string into substrings around the character). 4. `spreadTrim(str, ch)`: Removes leading and trailing characters from the string using a spread-based approach. **Library** The benchmark uses two libraries: 1. `RegExp` (regular expressions): used in `regexTrim`. 2. `Array.prototype.findIndex`, `Array.prototype.reverse`, `Array.prototype.push`, and `String.prototype.includes` ( Array methods): used in `indexTrim` and `spreadTrim`. **Options being compared** The benchmark compares the performance of each trimming function across different browsers (Firefox 111) on various devices. **Pros and Cons** Here's a brief overview of each approach: 1. **Index Trim (`indexTrim`)** * Pros: Simple, efficient, and works well for most use cases. * Cons: May not be as effective for very large strings or certain character sets. 2. **Regex Trim (`regexTrim`)** * Pros: Can handle a wide range of characters, including edge cases like newline and tab characters. * Cons: May be slower than `indexTrim` due to the overhead of regular expressions. 3. **Boolean Filter Trim (`booleanTrim`)** * Pros: Easy to understand and implement, works well for simple trimming tasks. * Cons: May not be as efficient as other approaches, especially for large strings. 4. **Spread Trim (`spreadTrim`)** * Pros: Simple and efficient, works well for most use cases. * Cons: May not be as effective for very large strings or certain character sets. **Other considerations** * The benchmark uses a simple test string `TestString` to evaluate the performance of each trimming function. This may not accurately represent real-world usage scenarios. * The benchmark only tests trimming functions with a single character, which may not reflect more complex use cases (e.g., trimming multiple characters or handling special cases like newline and tab characters). * The benchmark is designed for JavaScript engines, specifically Firefox 111. **Alternatives** Other approaches to trimming strings in JavaScript include: 1. Using `String.prototype.replace()` with a callback function. 2. Utilizing Unicode normalization techniques. 3. Implementing custom trimming functions using bitwise operations or other low-level optimizations. Keep in mind that these alternatives may have different performance characteristics and trade-offs, depending on the specific use case and requirements.
Related benchmarks:
Trimming leading/trailing characters Bounds
Trimming leading/trailing characters Bounds Fix
Trimming leading/trailing characters Bounds Fix2
Trimming leading/trailing characterss
Trimming leading/trailing characters again
Comments
Confirm delete:
Do you really want to delete benchmark?