Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
replaceAll no regex vs replace with regex
(version: 1)
Comparing performance of:
replaceAll no regex vs replace with regex
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sagittis urna luctus massa tincidunt pharetra quis in dolor. Integer tincidunt cursus mi, nec sagittis metus finibus in. Suspendisse vitae nulla quis turpis egestas tempus id sit amet tortor. Donec ut turpis a eros gravida ultricies. Duis gravida gravida lorem, condimentum lobortis sapien sodales sit amet. Duis sem neque, tincidunt et mauris pharetra, condimentum scelerisque velit. Quisque eu dui at eros tempor interdum suscipit eget urna. Cras ultrices dictum mi id aliquet. Nam quis nisl quam. Phasellus vulputate eu elit ut rutrum. Sed tempus eros consequat ante congue, et tempus velit sagittis. Maecenas aliquam imperdiet velit et luctus."
Tests:
replaceAll no regex
text.replaceAll("a", "b").replaceAll("c", "d").replaceAll("f", "g")
replace with regex
text.replace(/a/g, "b").replace(/c/g, "d").replace(/f/g, "g")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
replaceAll no regex
replace with regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
replaceAll no regex
786809.1 Ops/sec
replace with regex
530527.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark presented in the JSON comparing two approaches for string replacement in JavaScript tests the performance difference between using the `String.replaceAll()` method and using regular expressions with the `String.replace()` method. ### Benchmark Options 1. **replaceAll no regex** - **Benchmark Definition**: `text.replaceAll("a", "b").replaceAll("c", "d").replaceAll("f", "g")` - **Description**: This method leverages the `String.prototype.replaceAll()` function, introduced in ECMAScript 2021 (ES12). It replaces all occurrences of a specified string (without using regex) directly with another string. 2. **replace with regex** - **Benchmark Definition**: `text.replace(/a/g, "b").replace(/c/g, "d").replace(/f/g, "g")` - **Description**: This option uses `String.prototype.replace()` with a global regular expression. The `g` flag tells the regex to replace all occurrences of the specified pattern in the string. ### Pros and Cons #### replaceAll - **Pros**: - **Simplicity**: This method is straightforward for users not familiar with regex. The syntax clearly shows what is being replaced. - **Direct String Replacement**: It avoids the complexity and potential pitfalls of regex syntax, such as escaping special characters. - **Cons**: - **Limited Use Cases**: It can only replace strings and not patterns. Thus, if a more complex matching operation is needed, it won't be able to handle that without regex. #### replace with regex - **Pros**: - **Flexibility**: Regular expressions allow for complex pattern matching, enabling developers to perform more advanced string manipulations, such as replacing based on patterns (e.g., ignoring case, matching specific character classes). - **Cons**: - **Complexity**: Using regex can be intimidating for those unfamiliar with its syntax and behavior, leading to potential mistakes in the expression. - **Performance**: Depending on the complexity of the pattern and the implementation, regex operations can sometimes be slower than simple string replacements. ### Performance Results The benchmark results illustrate that `replaceAll` outperformed the regex replacement method in this test case: - **replaceAll no regex**: 587393.25 executions per second - **replace with regex**: 496239.47 executions per second This indicates that, for straightforward string replacement tasks, `replaceAll` offers better performance; however, its limitation in handling complex patterns may require adopting regex for those specific use cases. ### Alternatives Apart from the two methods tested, alternatives include: - **Using `Array.prototype.map()` or `Array.prototype.join()`**: You can split a string into an array, manipulate the elements, and then join them back into a string. This approach can be less performant for larger strings and doesn't inherently support pattern matching but can be useful in certain contexts. - **Third-Party Libraries**: Libraries like Lodash or underscore can offer additional utility functions for string manipulation that might simplify complex operations, albeit with a performance cost due to added overhead. ### Conclusion When choosing between `replaceAll` and `replace` with regex, it's essential to weigh the need for performance against the complexity of the string manipulation required. For simple straightforward replacements, `replaceAll` is highly effective; when advanced pattern matching is necessary, regex remains a powerful tool despite its potential drawbacks.
Related benchmarks:
regex vs includes speed comparison
regex vs includes javascript comparison
replaceAll vs regex replace v2
Reduce w/ Lowercase vs. Magic Regex
split-join vs regex replace - large input
RegEx.test vs. String.includes vs. String.match (long)
RegEx.test vs. String.includes vs. String.match vs String.indexOf (~2000 characters)
Split join vs replace long string long string
String.replace(RegEx) vs String.replaceAll(String)
Comments
Confirm delete:
Do you really want to delete benchmark?