Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ReplaceAll (regular expression, native API, split and join)
(version: 1)
Comparing performance of:
replace with regular expression vs replaceAll vs split and join
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Tests:
replace with regular expression
replaced = str.replace(/\ /g, "+");
replaceAll
replaced = str.replaceAll(" ", "+");
split and join
replaced = str.split(" ").join("+");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
replace with regular expression
replaceAll
split and join
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
replace with regular expression
334969.8 Ops/sec
replaceAll
342836.4 Ops/sec
split and join
592163.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Definition** The benchmark measures the performance of three different approaches to replace spaces in a string: 1. **Replace with regular expression (regex)**: This approach uses the `String.prototype.replace()` method with a regex pattern to replace all occurrences of spaces (`\\s`) with a plus sign (`+`). 2. **Native API (replaceAll)**: This approach uses the `String.prototype.replaceAll()` method to replace all occurrences of spaces with a plus sign. 3. **Split and join**: This approach splits the string into an array using the `String.prototype.split()` method, joins it back together with spaces, and then replaces each space with a plus sign. **Pros and Cons** * **Replace with regular expression (regex)**: + Pros: regex can be powerful for complex replacements. + Cons: regex can be slow and has a learning curve. * **Native API (replaceAll)**: + Pros: native to JavaScript, efficient, and easy to use. + Cons: may not work as expected for non-ASCII characters or edge cases. * **Split and join**: + Pros: simple and easy to understand. + Cons: may be slower than the other two approaches due to string manipulation. **Libraries** None of these options require a specific library, but if we consider the `replaceAll()` method, it's likely that this is a polyfill or implementation provided by the browser. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. It only uses standard ECMAScript functions and methods. **Other Alternatives** If you wanted to test alternative approaches, you could consider: * Using a different string manipulation library (e.g., `lodash`). * Implementing the replacement logic using a different algorithm (e.g., iterative instead of regex-based). * Testing with more complex input data (e.g., strings with non-ASCII characters or whitespace). **Benchmark Preparation Code** The provided preparation code creates a sample string (`str`) that contains spaces. This string is used as input for all three benchmarking approaches. **Individual Test Cases** Each test case defines a specific benchmark, which includes: * A `Benchmark Definition` string that describes the approach to be tested (e.g., "replaced = str.replace(/\\ /g, \"+\");"). * A `Test Name` that identifies the specific benchmark. * The original code that generates the input data and applies the replacement logic. The test cases use these definitions to execute the replacement logic on the sample string (`str`) and measure the performance of each approach.
Related benchmarks:
Unnecessary non-capturing groups
regex .replace() vs literal .replaceAll()
Javascript: Case insensitive string comparison performance 3
Array from vs string split with large strings
String.replace(RegEx) vs String.replaceAll(String)
Comments
Confirm delete:
Do you really want to delete benchmark?