Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
split-join vs regex replace vs replaceAll
(version: 0)
Comparing performance of:
replace regex vs split-join vs replaceAll
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
String.prototype.replaceAll = function(search, replacement) { var target = this; return target.replace(new RegExp(search, 'g'), replacement); };
Tests:
replace regex
"this is it".replace(/ /g, "+");
split-join
"this is it".split(" ").join("+");
replaceAll
"this is it".replaceAll(" ", "+");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
replace regex
split-join
replaceAll
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
replace regex
4159540.2 Ops/sec
split-join
3587522.0 Ops/sec
replaceAll
1239270.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark tests three different approaches to perform a string replacement: `split-join`, `regex replace`, and `replaceAll`. The goal is to determine which approach is the most efficient. **Options Compared** 1. **Split-Join**: This approach involves splitting the input string into an array using the specified separator, joining the array elements with a new delimiter, and then returning the resulting string. 2. **Regex Replace**: This approach uses regular expressions to replace all occurrences of a specific pattern in the input string. 3. **ReplaceAll**: This is a custom method that mimics the behavior of `replaceAll` from the JavaScript prototype chain. **Pros and Cons** * **Split-Join**: * Pros: Simple, intuitive, and easy to understand. * Cons: Can be slow for large strings due to the creation of an array and repeated string concatenation. * **Regex Replace**: * Pros: Fast and efficient for most cases, as it leverages optimized regular expression engines. * Cons: May have performance issues with complex patterns or very long strings. Additionally, some users might be unfamiliar with regex syntax. * **ReplaceAll**: * Pros: Custom implementation can potentially optimize for the specific use case, and avoids potential pitfalls of regex usage. * Cons: Requires custom implementation, which may lead to additional maintenance costs. **Library Usage** There is no explicit library mentioned in the provided code. However, it's essential to note that `replaceAll` uses a custom method, which might be influenced by the underlying JavaScript engine or browser. If you were to implement this on your own, you would likely use a similar approach as the one shown in the "Script Preparation Code". **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in this benchmark. **Other Alternatives** Some alternative approaches that might be worth considering include: * **String.prototype.replace()**: This is the standard, built-in method for replacing substrings. It's likely to perform well and is easy to understand. * **Array.prototype.map() + Array.prototype.join()**: This approach involves using `map()` to transform an array of characters into a new string by applying a lambda function to each element, and then joining the resulting array with the specified delimiter. These alternatives might offer different trade-offs in terms of performance, readability, or ease of implementation. However, without additional context or requirements, it's difficult to recommend one over the others. Keep in mind that the benchmark's primary goal is to compare the performance of these three approaches. The choice of alternative approach ultimately depends on your specific use case and priorities. Here's a simple example of how you could modify the "replace regex" test case using `String.prototype.replace()`: ```javascript { "Benchmark Definition": "\"this is it\".replace(/ /g, '+');", "Test Name": "replace" } ``` This will yield similar results as the original "replace regex" test case but might be slightly faster due to the optimized implementation of `String.prototype.replace()`.
Related benchmarks:
Simple Regex vs split/join
split-join vs regex replace
Split+join vs replaceAll
Replace spaces: split/join vs regex replace
Comments
Confirm delete:
Do you really want to delete benchmark?