Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Single vs multiple regex replace calls
(version: 0)
Comparing performance of:
Multiple, regex vs Single, regex vs Single, group regex vs No regex
Created:
8 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var str = "Et inventore ut est. Illum dolorem similique odit. Velit necessitatibus unde nostrum sequi ut pariatur suscipit sit magnam. Cum esse sed quod aut id. Recusandae vel commodi."
Tests:
Multiple, regex
str = str.replace(/a/g, '').replace(/e/g,'')
Single, regex
str = str.replace(/a|e/g, '')
Single, group regex
str = str.replace(/[ae]+/g, '')
No regex
str = str.replace('a', '').replace('e','')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Multiple, regex
Single, regex
Single, group regex
No regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Multiple, regex
8312667.5 Ops/sec
Single, regex
10252976.0 Ops/sec
Single, group regex
10259072.0 Ops/sec
No regex
10595482.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. The provided JSON represents a benchmark test case for comparing different approaches to regular expression replacement in JavaScript. The test compares four scenarios: 1. **Multiple regex replace calls**: This involves calling `str.replace()` twice, once with each individual character 'a' and then 'e', which is equivalent to using the `/a/g` and `/e/g` regex patterns. 2. **Single regex**: This involves calling `str.replace()` once with a single regex pattern '/a/' or '/e/', replacing both characters at once. 3. **Single group regex**: This involves calling `str.replace()` once with a regex pattern that uses capturing groups, `[ae]+`, which replaces both 'a' and 'e' characters in one operation. 4. **No regex**: This is the base case where no regular expression replacement is performed. Let's discuss each approach: * **Multiple regex replace calls**: * Pros: More accurate results for each individual character, as both replacements are done separately. * Cons: Requires more iterations and is potentially slower due to the extra function call. * **Single regex**: * Pros: Faster execution since only one `replace()` call is made, reducing overhead from multiple calls. * Cons: May lead to less accurate results if 'a' or 'e' are not unique characters in the input string. * **Single group regex**: * Pros: Similar performance benefits as single regex, with the added benefit of handling multiple characters more accurately due to capturing groups. * Cons: Requires more complex regex patterns for multi-character replacements, adding to complexity and potential overhead. * **No regex**: * Pros: Simplest approach with minimal execution overhead, especially beneficial if the replacement strings are short or few. * Cons: Results may be less accurate since no explicit replacement is done. Other alternatives could involve: * Using `str.split()` and then joining the resulting array of characters to remove 'a' and 'e'. * Implementing a custom loop through each character in the string, replacing them with an empty string. * Utilizing browser-specific features or optimizations that might be available but not captured by this benchmark. Regarding libraries used or special JavaScript features: None are explicitly mentioned in the provided JSON. The code relies solely on native JavaScript functions (`replace()`, `split()`).
Related benchmarks:
regex replaceAll vs regex replace with longer strings
String.replace(RegEx) vs String.replaceAll(String)
replaceAll vs regex replace pt2
replaceAll vs regex replace pt3
Comments
Confirm delete:
Do you really want to delete benchmark?