Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript string split vs match using regex v2
(version: 1)
Comparing performance of:
split * 6 vs match * 6 vs replace * 6
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script> var getRandomIntegerInclusive = (min, max) => { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min + 1)) + min; } var arr1 = "\u200b\xA0\x20\n".split(""); var str1 = "This benchmark is testing if split or match is faster on a string using regex"; var str2 = str1.replace(/\S/g, () => arr1[getRandomIntegerInclusive(0, 3)]); var regex = /[\u200b\xA0\x20\n]/g; var pxd = () => `${Math.floor(Math.random() * 314159265359 + 314159265359).toString(36)}` </script>
Tests:
split * 6
`${str2}${pxd()}`.split(regex); `${str2}${pxd()}`.split(regex); `${str2}${pxd()}`.split(regex); `${str2}${pxd()}`.split(regex); `${str2}${pxd()}`.split(regex); `${str2}${pxd()}`.split(regex);
match * 6
`${str2}${pxd()}`.match(regex); `${str2}${pxd()}`.match(regex); `${str2}${pxd()}`.match(regex); `${str2}${pxd()}`.match(regex); `${str2}${pxd()}`.match(regex); `${str2}${pxd()}`.match(regex);
replace * 6
`${str2}${pxd()}`.replace(regex,''); `${str2}${pxd()}`.replace(regex,''); `${str2}${pxd()}`.replace(regex,''); `${str2}${pxd()}`.replace(regex,''); `${str2}${pxd()}`.replace(regex,''); `${str2}${pxd()}`.replace(regex,'');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
split * 6
match * 6
replace * 6
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:134.0) Gecko/20100101 Firefox/134.0
Browser/OS:
Firefox 134 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
split * 6
70921.3 Ops/sec
match * 6
69093.1 Ops/sec
replace * 6
68946.6 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark compares the performance of three different string manipulation methods in JavaScript: `split` using a regular expression, `match` using a regular expression, and `replace` using a regular expression. Each of these methods is applied to a string that is constructed dynamically in the preparation code. ### Description of Options Compared 1. **Split Method**: - **Test Case**: `split * 6` - **Usage**: The benchmark utilizes the `split` method on the string `str2`, combining it with a dynamically generated string from `pxd()`, and splits it based on the predefined regular expression `regex`. - **Purpose**: To segment the string into an array based on specific delimiters defined by the regex. 2. **Match Method**: - **Test Case**: `match * 6` - **Usage**: This test case leverages the `match` method to find all occurrences of patterns specified in the regex from `str2`. - **Purpose**: To retrieve an array of all matches of the specified regex pattern in the string. 3. **Replace Method**: - **Test Case**: `replace * 6` - **Usage**: Here, the `replace` method is used to remove all occurrences of the characters defined in the regex by replacing them with an empty string. - **Purpose**: To effectively filter out characters from the string based on the regex, modifying the original string. ### Pros and Cons of Each Approach - **Split**: - **Pros**: Efficiently breaks down a string into parts, making it useful when individual segments are needed. - **Cons**: When the goal is not to extract segments but rather to identify characters, using `split` may be unnecessary and less efficient compared to direct pattern matching. - **Match**: - **Pros**: Directly retrieves matches of a regex pattern, which is beneficial for pattern recognition and extraction. This method is generally clearer when the goal is to find specific substrings. - **Cons**: May be less performant if the string is large and numerous matches exist, as it requires searching through the whole string. - **Replace**: - **Pros**: Useful for cleaning up or modifying strings, especially for tasks requiring removal of unwanted characters or substrings. - **Cons**: For cases where only the presence of characters is needed, replacing may be an overcomplicated solution. ### Other Considerations - The benchmarks use a randomly generated string `pxd()` along with `str2` to ensure the input string varies across tests, which helps in simulating real-world scenarios where string contents can differ. - The regular expression `[\\u200b\\xA0\\x20\\n]` includes the zero-width space, non-breaking space, space, and newline characters. This regex is effective for scenarios where whitespace or invisible characters may be present in and need to be handled in a string. ### Alternatives - Alternatives to these methods could include using string manipulation libraries, such as `lodash` or native JavaScript functions like `String.fromCodePoint()` combined with array methods for more complex operations. - For performance-sensitive applications, there may be custom implementations that avoid regex altogether, especially in scenarios where regex engines can introduce overhead. Overall, this benchmark provides insight into the performance differences between these common string manipulation techniques in JavaScript, helping developers choose the appropriate method based on their specific needs for string processing.
Related benchmarks:
Split strings by character count (regex vs. slice vs. substr)
Split vs Regex
Regex vs .indexOf vs .startsWith vs .substr (full)
Regex vs .indexOf vs .startsWith vs .substr
Split vs Regex Iteration
Split vs Regex vs Replace Iteration
regex vs RegExp vs indexof vs indexOf and substr
Split vs Regex 2
Split vs Regex tes cstom
Comments
Confirm delete:
Do you really want to delete benchmark?