Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
replaceAll vs regex with g flag
(version: 0)
Comparing performance of:
replaceAll vs regex
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.hugeString = ""; for (let i = 0; i < 10000; ++i) { window.hugeString += "abcdefghijklmnopqrstuvwxyz"; }
Tests:
replaceAll
let newString = window.hugeString.replaceAll("m", ".");
regex
let newString = window.hugeString.replace(/m/g, ".");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
replaceAll
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
replaceAll
2405.3 Ops/sec
regex
3878.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested on MeasureThat.net. The provided JSON represents a JavaScript benchmark, specifically comparing the performance of two approaches: `String.prototype.replaceAll()` and regular expressions with the global flag (`/regex with g flag`). **Options Compared:** 1. **replaceAll**: Uses the built-in `String.prototype.replaceAll()` method to replace all occurrences of a substring. 2. **regex (with g flag)**: Uses a regular expression with the global flag (`/g`) to replace all occurrences of a pattern. **Pros and Cons:** * **replaceAll**: * Pros: Typically faster, more intuitive for simple replacements, and generally supported by most browsers. * Cons: May not handle edge cases correctly or be as efficient for complex patterns. * **regex (with g flag)**: * Pros: More flexible for complex patterns and can handle edge cases accurately. However, it may be slower than `replaceAll` due to the overhead of regex parsing and matching. * Cons: Can be less intuitive for simple replacements, and not all browsers support the global flag. **Library Used:** None The benchmark does not rely on any external libraries. **Special JS Feature or Syntax:** The use of the global flag (`/g`) in regular expressions is a standard JavaScript feature. This allows regex patterns to match all occurrences within a string, rather than just the first one. **Benchmark Preparation Code:** The provided code creates a large string `window.hugeString` and appends it 10,000 times to itself. This generates a very large string that will be used for the benchmark. Here's an explanation of how this helps with performance testing: * Creating such a long string is computationally expensive but provides a realistic scenario for measuring replacement speed. * By appending to the string multiple times, we ensure that our replacements are not optimized away by modern JavaScript engines (which might cache results or perform some optimizations). **Alternatives:** Some alternative methods to compare include: 1. **String.prototype.replace() without flags**: This method doesn't have a global flag like `g`, but still handles single and multi-character replacements in the same manner as `replaceAll`. 2. **Using a regex with a different modifier (e.g., `/m` for line endings)**: These might be useful for specific use cases, but are generally less common than the global flag. 3. **String.prototype.split() followed by String.prototype.join()**: This could potentially be faster if done right (for example, if all characters were replaced simultaneously, which is not possible with this method). 4. **Implementing your own custom string replacement function**: Creating a highly optimized implementation can sometimes outperform existing standard methods but requires more developer effort. In conclusion, `replaceAll` vs regex with the global flag are two common approaches used in JavaScript for replacing substrings from strings. While each has its pros and cons, the global flag provides flexibility and accuracy, making it more suitable for handling complex patterns.
Related benchmarks:
Global matching regex with and without + char
regex replace vs replaceAll vs replace in loop V1.1
string char delete perfomance
Remove first symbol from string
Comments
Confirm delete:
Do you really want to delete benchmark?