Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
regex replace vs split vs loop
(version: 0)
Comparing performance of:
replace vs split
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis a';
Tests:
replace
const result = str.replace(/\belitr\b/g, 'troy');
split
const result = str.split(' ').map(value => value === 'elitr' ? 'troy' : value).join(' ');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
replace
split
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/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
replace
391280.6 Ops/sec
split
167910.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The benchmark defines three test cases: `regex replace`, `split`, and `loop`. The script preparation code is a long string containing only whitespace and punctuation characters. This string will be used to measure the performance of each test case. **Options Compared** In this benchmark, we have two options being compared: 1. **Regex Replace**: This option uses the `replace()` method with a regular expression (regex) pattern to replace specific substrings in the input string. 2. **Split and Join**: This option splits the input string into an array of words using the `split()` method and then joins them back together using the `join()` method, replacing certain substrings. **Pros and Cons** * **Regex Replace**: * Pros: * More flexible and powerful than `split` and `join`, as it can perform complex substitutions. * Can handle non-word characters (e.g., punctuation, symbols) more efficiently. * Cons: * Generally slower due to the overhead of compiling a regex pattern. * May be less intuitive for simple string replacement tasks. * **Split and Join**: * Pros: * More lightweight than `replace()` and can be faster for large strings. * Easier to understand and use for simple string manipulation tasks. * Cons: * Less flexible and powerful than `replace()`, as it only replaces exact word matches. * Can be slower if the input string is very long. **Library Used** There is no library explicitly mentioned in the benchmark definition or test cases. However, JavaScript's built-in functions like `split()` and `join()` are used extensively throughout the code. **Special JS Feature or Syntax** None of the special JS features or syntaxes (e.g., async/await, arrow functions) are used in this benchmark. **Other Alternatives** If you were to write a similar benchmark for a different string manipulation task, some possible alternatives could be: * **Buffer manipulation**: If you wanted to measure the performance of operations on binary data (e.g., images, audio files), you might use `Buffer` objects instead of strings. * **Array methods**: You could compare the performance of using `map()` and other array methods for string manipulation instead of `split()` and `join()`. * **Regex alternatives**: Depending on your specific requirements, you might choose to use alternative regex libraries or tools (e.g., `unicode-matcher`) that offer better performance or features. Keep in mind that these alternatives would require modifications to the benchmark definition and test cases.
Related benchmarks:
regex .replace() vs literal .replaceAll()
TextEncoder.encode() vs encodeURIComponent
Javascript: Case insensitive string comparison performance 3
String.replace(RegEx) vs String.replaceAll(String)
Comments
Confirm delete:
Do you really want to delete benchmark?