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
llama3.1:latest
, generated one year ago):
Let's dive into the details of this benchmark. **Benchmark Description** The benchmark compares three different approaches to replace all spaces in a large string with a plus sign (+). The test case uses a long paragraph of Latin text as the input string (`targetString`). **Test Cases** There are three individual test cases: 1. **Replace Regex**: This test case uses the `replace()` method with a regular expression (regex) to replace all spaces in the string. * Code: `targetString.replace(/ /g, "+");` * Library/Feature: Uses JavaScript's built-in regex capabilities 2. **Split-Join**: This test case splits the input string into an array of words using the `split()` method, then joins them back together with a plus sign (+) using the `join()` method. * Code: `targetString.split(" ").join("+");` * Library/Feature: Uses JavaScript's built-in `split()` and `join()` methods 3. **ReplaceAll Regex**: This test case uses the newer `replaceAll()` method (introduced in ECMAScript 2020) to replace all spaces in the string. * Code: `targetString.replaceAll(/ /g, "+");` * Library/Feature: Uses JavaScript's built-in `replaceAll()` method **Benchmark Results** The latest benchmark results show that: * **Split-Join** achieves an execution speed of 30458.1015625 executions per second (EPS) on a mobile device running Firefox 98. * **Replace Regex** achieves an execution speed of 28637.119140625 EPS on the same device and browser combination. * **ReplaceAll Regex** achieves an execution speed of 28094.056640625 EPS, which is slightly slower than the other two approaches. **Pros/Cons** The pros and cons of each approach are: * **Replace Regex**: Pros - simple code, uses built-in regex capabilities; Cons - may be slower due to regex processing. * **Split-Join**: Pros - avoids regex overhead, easy to understand; Cons - creates an array of words before joining them back together, which can be inefficient for large strings. * **ReplaceAll Regex**: Pros - modern API, efficient for large strings; Cons - not supported in older browsers or environments. **Considerations** When choosing between these approaches, consider the following: * If you need to support very old browsers or environments (pre-ECMAScript 2020), use `replace()` with a regex. * For modern codebases and devices, use `replaceAll()` for better performance. * If you prefer a simple, easy-to-understand approach, go with `split()` and `join()`, but be aware of potential efficiency issues. **Alternatives** If these approaches don't meet your needs, consider using other string manipulation techniques, such as: * Using a string replacement library or utility function * Implementing a custom string replacement algorithm * Exploring alternative programming languages or environments with optimized string processing capabilities
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?