Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native string replaceAll vs regex replace
(version: 1)
Comparing performance of:
replaceAll vs replace vs replaceAll (regex)
Created:
10 months ago
by:
Guest
Go to the latest result
Tests:
replaceAll
"this is it".replaceAll(" ", "+");
replace
"this is it".replace(/ /g, "+");
replaceAll (regex)
"this is it".replaceAll(/ /g, "+");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
replaceAll
replace
replaceAll (regex)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
replace
5.9M/s
replaceAll
5.2M/s
replaceAll (regex)
4.0M/s
View exact numbers
Test name
Executions per second
✓
replace
5,932,414 Ops/sec
replaceAll
5,227,379 Ops/sec
replaceAll (regex)
4,031,235 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 10 months ago):
The benchmark provided compares the performance of three different methods of replacing characters in a string in JavaScript: `replaceAll` with a string argument, `replace` with a regular expression, and `replaceAll` with a regular expression. ### Tested Options: 1. **String `replaceAll` Method:** - **Benchmark Definition:** `"this is it".replaceAll(" ", "+")` - **Description:** This method replaces all instances of the specified substring (in this case, a space) with another substring (a plus sign `+`). - **Performance Result:** 8,839,795 executions per second. - **Pros:** - Simple and direct when replacing static substrings. - Readable and concise syntax. - **Cons:** - Cannot use regular expressions directly, limiting its flexibility compared to regex-based methods. 2. **Regex `replace` Method:** - **Benchmark Definition:** `"this is it".replace(/ /g, "+")` - **Description:** This method uses a regular expression to find all instances of spaces (`/ /g`, where `g` stands for "global") and replaces them with a plus sign. - **Performance Result:** 1,635,206.125 executions per second. - **Pros:** - Very powerful and flexible; can match complex patterns beyond simple substrings. - **Cons:** - Slightly more complex syntax and less performant for simple tasks compared to `replaceAll`. - Regular expressions can be harder to read and maintain for those unfamiliar with regex syntax. 3. **Regex `replaceAll` Method:** - **Benchmark Definition:** `"this is it".replaceAll(/ /g, "+")` - **Description:** This attempts to use `replaceAll` with a regex pattern to match spaces. - **Performance Result:** 1,319,616.875 executions per second. - **Pros:** - Combines the benefits of both methods by allowing regex while maintaining the simplicity of `replaceAll`. - **Cons:** - Older JavaScript environments may not support `replaceAll` with regex well, which could lead to compatibility issues. - Potentially confusing for users who expect `replaceAll` to only accept strings. ### Other Considerations: - The benchmark results clearly show that the native string method `replaceAll` has superior performance compared to both regex methods. This is informally indicative of how optimized specific string methods can be in JavaScript engines. - Regular expressions, while powerful, involve overhead that can lead to decreased performance—especially if the operation does not need to handle complex patterns. ### Alternatives: - **Using a Simple Loop**: For very basic replacements, one might also consider using a loop that constructs a new string. However, this approach is typically less performant than built-in methods and usually more verbose. - **String Libraries**: Consider using libraries like Lodash or Underscore.js, which can encapsulate string functionality, though this may add unnecessary weight if you only need a single string operation. - **Template Literals or String Interpolation**: While not directly relevant to replacing strings, these features enable building strings in a more readable way when concatenating static values. These benchmarks allow developers to choose the most efficient method based on specific use cases while considering the trade-offs related to readability, maintainability, and performance.
Related benchmarks
replaceAll vs regex replace (no prep code)
replaceAll vs replace with /g
Global regex replace vs replaceAll
replace vs replaceAll v3000
replaceAll vs regex replace TEST
ReplaceAll vs Replace with regex
replace vs replaceAll (native)
replaceAll vs regex replace native
Comments
Confirm delete:
Do you really want to delete benchmark?