Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Trimming leading/trailing string of characters
(version: 0)
Comparing performance of:
Index Version (Jason Larke) vs Regex Version (leaf)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var TestString = " ,".repeat(200) + '='.repeat(20000) + " ,".repeat(200); var regexTrim = function(str, str2) { return str.replace(/^[ ,]+|[ ,]+$/g, ''); }; function indexTrim(str, ch) { var start = 0, end = str.length; while(start < end && (str[start] === ch[0] || str[start] === ch[1])) ++start; while(end > start && (str[end - 1] === ch[0] || str[end - 1] === ch[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, " ,")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Index Version (Jason Larke)
Regex Version (leaf)
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 break down the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark definition represents a JavaScript function that trims leading and trailing characters from a string. There are four different implementations: 1. `indexTrim(str, ch)`: This function uses a simple iterative approach to find the first and last occurrences of the character in the string. 2. `regexTrim(str, str2)`: This function uses a regular expression to match and replace the leading and trailing characters from the string. 3. `booleanTrim(str, ch)`: This function uses the `split()` method with a regular expression to split the string into an array of substrings, filters out empty strings using `Boolean`, and then joins them back together using the same separator. 4. `spreadTrim(str, ch)`: This function uses the spread operator (`...`) to find the index of the character in the string, reverses the string, finds the last occurrence of the character, and then returns a substring from the original string starting at the first occurrence. **Comparison** The benchmark compares the performance of these four implementations on the same test case: * `TestString`: A long string containing leading and trailing characters (`" ,".repeat(200) + '='.repeat(20000) + " ,".repeat(200)`). * The character to trim: `" ,"`. **Pros and Cons** Here's a brief summary of each implementation's pros and cons: 1. `indexTrim(str, ch)`: This approach is simple and efficient but may not be suitable for larger strings or more complex trimming scenarios. * Pros: Simple, fast * Cons: May not work for larger strings or multiple characters to trim 2. `regexTrim(str, str2)`: This approach uses a regular expression to match and replace the leading and trailing characters from the string. * Pros: Works with any character sequence, relatively efficient * Cons: Requires regular expressions, may be slower than native implementation 3. `booleanTrim(str, ch)`: This approach uses the `split()` method to split the string into substrings, filters out empty strings, and then joins them back together. * Pros: Works with any character sequence, relatively efficient * Cons: May create temporary arrays and objects, slower than native implementation 4. `spreadTrim(str, ch)`: This approach uses the spread operator to find the index of the character in the string, reverses the string, finds the last occurrence of the character, and then returns a substring. * Pros: Works with any character sequence, relatively efficient * Cons: Requires modern JavaScript features (spread operator), may be slower than native implementation **Library and Special JS Features** The benchmark uses the `split()` method, which is a built-in JavaScript method. The `repeat()` method, `indexOf()`, and `substring()` methods are also used. The benchmark does not use any external libraries or special JavaScript features beyond what's required for the implementations themselves. **Other Alternatives** If you want to optimize string trimming in JavaScript, you may consider using: * `String.prototype.trim()`: A built-in method that removes leading and trailing whitespace characters. * `String.prototype.replace()` with a regular expression: Can be used to remove specific characters from the start and end of a string. * `Array.prototype.slice()`: Can be used to extract a substring from an array or string. Keep in mind that the performance of these alternatives may vary depending on the specific use case and browser.
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?