Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String.replace(RegEx) vs String.replaceAll(String)
(version: 0)
Comparing performance of:
RegEx.test vs String.includes
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum!"; var regex = /sed\sdo/g;
Tests:
RegEx.test
string.replace(regex, 'do sed');
String.includes
string.replaceAll('sed do', 'do sed')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
796579.9 Ops/sec
String.includes
708067.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Definition** The benchmark is comparing two string replacement methods: 1. `String.replace(RegEx)` 2. `String.replaceAll(String)` **What are they?** * `String.replace(RegEx)` replaces all occurrences of a regular expression (`regex`) with a replacement string. + In the provided benchmark, `regex` is `/sed\sdo/g`. This regular expression matches "sed" followed by any whitespace characters and then "do". + The replacement string is `'do sed'`. * `String.replaceAll(String)` replaces all occurrences of a substring (in this case, "sed do") with another substring. + In the provided benchmark, the substrings are replaced as follows: "sed do" -> "do sed". **Comparison** The benchmark aims to measure which method is faster. The tests compare the execution times of both methods on a given string (`string`) containing multiple occurrences of the regular expression or substring. **Pros and Cons** * `String.replace(RegEx)`: + Pros: Can be more efficient for larger strings, as it uses an optimized algorithm to replace matches. + Cons: Requires specifying the regular expression pattern, which can be error-prone and less readable. * `String.replaceAll(String)`: + Pros: More readable and easier to maintain, as no regular expression is required. + Cons: May be slower for large strings due to the need to scan the entire string. **Other Considerations** Both methods have limitations: * `String.replace(RegEx)` may not work correctly if the replacement string contains special characters or formatting. * `String.replaceAll(String)` can be less efficient if the replaced substring is long, as it requires creating a new substring for each match. **Library Usage** The benchmark uses no explicit libraries. However, it's likely that the JavaScript engine being tested (e.g., V8) provides its own optimized implementation of string replacement methods. **Special JS Features/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. **Alternatives** If you were to rewrite this benchmark using a different approach: * Instead of using `String.replace(RegEx)` and `String.replaceAll(String)`, you could use native WebAssembly instructions (e.g., `WASM`), which might provide better performance. * You could also explore other string replacement libraries or frameworks, such as `lodash` or `string-patch`. Keep in mind that the choice of approach depends on your specific requirements, target audience, and desired level of optimization.
Related benchmarks:
regex .replace() vs literal .replaceAll()
Javascript: Case insensitive string comparison performance 3
replaceAll vs regex replace pt2
replaceAll vs regex replace pt3
Comments
Confirm delete:
Do you really want to delete benchmark?