Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Native replaceAll vs regex replace vs replace with String.raw
(version: 0)
Comparing performance of:
replace regex vs replace All vs replace regex with String.raw
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
replace regex
"Hello, [%user_name%]! What's up, [%user_name%]?".replace(/\[\%user_name\%\]/g, 'Jane');
replace All
"Hello, [%user_name%]! What's up, [%user_name%]?".replaceAll(`[%user_name%]`, 'Jane');
replace regex with String.raw
"Hello, [%user_name%]! What's up, [%user_name%]?".replace(new RegExp(String.raw`\[\%user_name\%\]`, "g"), 'Jane');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
replace regex
replace All
replace regex with String.raw
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 118 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
replace regex
3738516.8 Ops/sec
replace All
3210140.0 Ops/sec
replace regex with String.raw
637968.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON and explain what's being tested, the different approaches, their pros and cons, and other considerations. **Benchmark Definition** The `Name` is "Native replaceAll vs regex replace vs replace with String.raw". This benchmark compares three different methods of replacing a placeholder in a string: 1. **Native `replaceAll()`**: Using the built-in JavaScript method `replaceAll()`. 2. **Regex Replace**: Using regular expressions to replace the placeholder. 3. **Replace with `String.raw`**: Using the `String.raw` syntax to create a template string. **Individual Test Cases** There are three test cases: 1. **"replace regex"** ```json "Benchmark Definition": "\"Hello, [%user_name%]! What's up, [%user_name%]?\".replace(/\\[\\%user_name\\%\\]/g, 'Jane');", ``` This test case uses regular expressions to replace the `%user_name%` placeholder with `Jane`. The `/\\[\\%user_name\\%\\]/g` pattern matches any occurrence of `%user_name%` anywhere in the string, and replaces it with `Jane`. 2. **"replace All"** ```json "Benchmark Definition": "\"Hello, [%user_name%]! What's up, [%user_name%]?\".replaceAll(`[%user_name%]`, 'Jane');", ``` This test case uses the `replaceAll()` method to replace the `%user_name%` placeholder with `Jane`. The `String.raw` syntax is used to create a template string. 3. **"replace regex with String.raw"** ```json "Benchmark Definition": "\"Hello, [%user_name%]! What's up, [%user_name%]?\".replace(new RegExp(String.raw`\\[\\%user_name\\%\\]`, \"g\"), 'Jane');", ``` This test case uses regular expressions to replace the `%user_name%` placeholder with `Jane`. The `String.raw` syntax is used to create a template string, and the `RegExp` constructor is used to create a regex pattern. **Pros and Cons** 1. **Native `replaceAll()`**: * Pros: Fast, efficient, and widely supported. * Cons: May not work as expected with complex patterns or edge cases. 2. **Regex Replace**: * Pros: Flexible, powerful, and can handle complex patterns. * Cons: Can be slow, especially for large strings or complex patterns. 3. **Replace with `String.raw`**: * Pros: Fast, efficient, and easy to use. * Cons: May not work as expected with complex patterns or edge cases. **Library and Purpose** There is no explicit library mentioned in the benchmark definition. However, it's worth noting that JavaScript template literals (e.g., `String.raw`) are a built-in feature of the language, and do not rely on external libraries. **Special JS Feature/Syntax** The `String.raw` syntax is used to create template strings, which allows for more efficient string creation and manipulation. This syntax was introduced in ECMAScript 2015 (ES6) as part of the Template Literals feature. **Other Alternatives** If you need to replace placeholders in a string, other alternatives include: 1. **Using a library like Moment.js**: For date formatting and manipulation. 2. **Using a templating engine like Handlebars**: For complex template rendering. 3. **Using a regex library like Regexr**: For advanced regex patterns. However, for simple use cases, the native JavaScript methods (`replaceAll()` or template literals) are usually sufficient and efficient.
Related benchmarks:
Regex Replace vs native replaceAll
replaceAll vs replace with regex for empty string substition
replaceAll native 2023 vs regex replace
replaceAll vs rgx replace
replaceAll vs regex replace native
Comments
Confirm delete:
Do you really want to delete benchmark?