Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex vs split with regex
(version: 3)
Comparing performance of:
split vs regex
Created:
6 years ago
by:
Registered User
Jump to the latest result
Tests:
split
const str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; const count = str.split(/\W+/).length
regex
const str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."; var regEx = /([^\u0000-\u007F]|\w)+/g; const count = str.match(regEx).length;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
split
regex
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):
**Overview of the Benchmark** The provided benchmark compares the performance of two approaches to process a string: splitting the string using the `split()` method with a regular expression (`/\\W+/`) and using regular expressions (regex) with a pattern (`([^\\u0000-\\u007F]|\\w)+/g`). **Test Case 1: Split** In this test case, the string is split into an array of substrings using the `split()` method with a regex pattern `/\\W+/`. The `\\W` matches any non-word character (equivalent to `[^\w_]`), and the `+` quantifier repeats the match as many times as possible. **Pros:** 1. **Simplistic and readable**: The code is easy to understand, as it uses a well-known method for splitting strings. 2. **Fast**: The `split()` method is generally fast, especially when compared to regex-based approaches. **Cons:** 1. **Limited flexibility**: The `\\W` pattern only matches non-word characters, which might not be suitable for all use cases. 2. **Potential performance issues**: If the input string contains many consecutive non-word characters, the `split()` method might create an array with a large number of empty strings. **Test Case 2: Regex** In this test case, the string is processed using regex with the pattern (`([^\\u0000-\\u007F]|\\w)+/g`) to match any character that is not a word (or `\\w`). The `^` character inside the square brackets negates the match, effectively matching only non-word characters. **Pros:** 1. **Flexible**: This approach can be used to match various types of non-word characters. 2. **Accurate**: By using a more complex pattern, this method can handle edge cases that might not be caught by the `split()` method. **Cons:** 1. **More complex and harder to read**: The regex pattern is less intuitive and may require additional explanations or comments. 2. **Potential performance issues**: Regex-based approaches can be slower than simple string methods like `split()`, especially for large input strings. **Library Used: None** There are no libraries used in these test cases, as the comparison focuses on built-in JavaScript methods (`split()`). **Special JS Feature/Syntax:** None of the provided benchmark tests use any special JavaScript features or syntax beyond standard regex patterns and method calls. **Other Alternatives** If you wanted to compare other approaches to process a string, some alternatives could be: 1. **Using `replace()` instead of `split()`**: This would involve replacing all non-word characters with an empty string (`''`). 2. **Using `Array.prototype.map()`**: This would involve creating a new array by applying a mapping function to each element in the original string. 3. **Using a custom implementation**: You could write your own custom algorithm to process the string, potentially using bitwise operations or other low-level techniques. Keep in mind that these alternatives might have different performance characteristics and trade-offs compared to the `split()` and regex approaches used in this benchmark.
Related benchmarks:
str.match vs str.split.pop
str.match vs str.Split(regex)
Regex vs Split Time
string.split(RegExp); vs string.split(string);
JavaScript string split vs match using regex
Comments
Confirm delete:
Do you really want to delete benchmark?