Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
split-join vs regex replace - large input
(version: 0)
Comparing performance of:
replace regex vs split-join vs replaceAll regex
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var targetString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla at magna nisi. Pellentesque tempus, ligula in dictum tempor, lacus diam dictum dolor, vel semper justo libero et ex. Aenean vestibulum malesuada urna, at vestibulum felis posuere non. Morbi interdum erat id est mollis, id efficitur sapien efficitur. In ut bibendum velit. Etiam interdum neque sed tincidunt vestibulum. In hac habitasse platea dictumst. Suspendisse sed consequat mauris. Nullam facilisis et massa in rhoncus. Aliquam erat volutpat. Sed eu ante suscipit, vulputate tortor non, ultricies dolor. Aliquam nisl est, dignissim vel egestas vitae, malesuada vitae urna. Vestibulum vitae dignissim sapien. Donec nec lectus at sem efficitur eleifend lobortis a turpis. Etiam posuere mauris nunc, et ullamcorper ante dapibus quis. Etiam urna urna, lobortis eget posuere at, tincidunt ut tortor. Morbi quis faucibus augue. Suspendisse vel est facilisis lectus ultricies eleifend. Nulla convallis elit et pulvinar consectetur. In eget dictum eros. Praesent auctor elementum nunc nec eleifend. Mauris vel felis ante. Mauris sit amet ex rutrum dolor ornare fringilla. Cras semper accumsan magna, nec mattis orci rhoncus eu. Phasellus at libero dolor. Maecenas nec urna nulla. Mauris et tellus cursus, egestas mi quis, sagittis lacus. Suspendisse luctus ultricies eros eget commodo. In vulputate efficitur urna ac ornare. Integer aliquam dolor nec odio venenatis, vitae auctor enim consequat. Nullam facilisis eu mauris a consequat. Sed sollicitudin, lectus in suscipit maximus, nulla dui viverra risus, ac bibendum arcu lorem laoreet sapien. Sed maximus gravida eros tincidunt viverra. Fusce quis nunc eu dui pulvinar efficitur. In hac habitasse platea dictumst. Fusce ligula mauris, varius in dictum vel, ultricies nec purus. Nullam ut neque elit. Fusce a purus hendrerit, hendrerit lectus quis, vestibulum ex." //Credits to lipsum.com for the text
Tests:
replace regex
targetString.replace(/ /g, "+");
split-join
targetString.split(" ").join("+");
replaceAll regex
targetString.replaceAll(/ /g, "+");
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 regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares three different ways to replace all spaces in a large string with the plus sign (`+`). Let's break down each approach: **1. `targetString.replace(/ /g, \"+\");`:** This uses a regular expression (`/ /g`) to find all occurrences of a space character (` `) globally (`g`) and replaces them with `"+"`. Regular expressions can be powerful for complex pattern matching, but they can also introduce overhead. **2. `targetString.split(\" \").join(\"+\");`:** This approach splits the string into an array of words using spaces as delimiters (`split(" " )`). Then, it joins the array elements back together using `"+"` as a separator (`join("+")`). This is a more straightforward method but might be less performant for very large strings. **3. `targetString.replaceAll(/ /g, \"+\");`:** Introduced in ES2021 (JavaScript version 18), this method offers a simpler syntax for replacing all occurrences of a pattern with a replacement string. It's often considered more readable and potentially more performant than using `replace` with a regular expression. **Pros and Cons:** * **Regular Expressions (`replace`)**: * **Pros:** Powerful for complex pattern matching, can handle various scenarios. * **Cons:** Can introduce overhead due to parsing and regex engine work. * **Split-Join**: * **Pros:** Simple and readable. * **Cons:** Might be less efficient for large strings compared to other methods. * **`replaceAll`**: * **Pros:** Simpler syntax, potentially more performant than `replace`. * **Cons:** Only available in newer JavaScript versions (ES2021 and above). **Alternatives:** While these three methods are common approaches, other options exist depending on the specific context: * Using a dedicated string manipulation library might offer optimized performance. However, adding external dependencies should be weighed against potential benefits. * If the input is very large, consider processing it in chunks to reduce memory usage and improve performance. This benchmark highlights the trade-offs between different approaches for string manipulation in JavaScript. The best choice often depends on factors like the size of the input data, performance requirements, code readability, and support for newer JavaScript features.
Related benchmarks:
Lodash Replace/Split VS JS Replace/Split
str split vs spread (LONG STRINGS) v1
RegEx.test vs. String.includes vs. String.match (long)
Split join vs replace long string long string
Comments
Confirm delete:
Do you really want to delete benchmark?