Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array from vs string split with large strings
(version: 0)
Comparing performance of:
Array.from vs string split
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var s1 = "foo"; var s2 = "foo 12312"; var s3 = `foo 12312 foo 12312foo 12312foo 12312vunsdf8 "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." Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?" 1914 translation by H. Rackham "But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?`;
Tests:
Array.from
let n1 = Array.from(s1); let n2 = Array.from(s2); let n3 = Array.from(s3);
string split
let n1 = s1.split(''); let n2 = s2.split(''); let n3 = s3.split('');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.from
string split
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 benchmark definition and test cases. **Benchmark Definition** The benchmark is called "Array from vs string split with large strings". It tests the performance of two different methods for converting a large string into an array: `Array.from()` and string splitting (`split()`) using a single character as the separator. **Test Cases** There are two individual test cases: 1. **"Array.from"**: This test case uses the `Array.from()` method to create three arrays from the strings `s1`, `s2`, and `s3`. The code is: ```javascript let n1 = Array.from(s1); let n2 = Array.from(s2); let n3 = Array.from(s3); ``` In this case, `Array.from()` creates a new array by iterating over the string characters. 2. **"string split"**: This test case uses the `split()` method to create three arrays from the strings `s1`, `s2`, and `s3` using a single character as the separator (`''`). The code is: ```javascript let n1 = s1.split(''); let n2 = s2.split(''); let n3 = s3.split(''); ``` In this case, `split()` creates a new array by splitting the string into substrings at each occurrence of the specified separator. **Library and Features** No specific library or JavaScript feature is being tested in these benchmark cases. The code uses standard JavaScript methods (`Array.from()` and `split()`) to perform the comparisons. **Pros and Cons of Each Approach** Both approaches have their pros and cons: * **Array.from()**: This method is generally faster and more efficient for large strings, as it doesn't require creating intermediate string variables or iterating over the entire string. However, it may be slower for small strings or when working with very long strings that don't fit in memory. * **String Splitting**: This approach can be slower than `Array.from()` for large strings, especially if you're splitting on a short separator like an empty string. However, it can be more memory-efficient and flexible when dealing with complex string patterns. **Alternatives** Other alternatives to consider: * Using regular expressions (regex) to split the string, which can provide more control over the splitting process but may be slower. * Iterating over the string using a `for...of` loop or `forEach()` method to create an array, which can be slower than both `Array.from()` and string splitting. **Latest Benchmark Result** The latest benchmark result shows that string splitting (`split()`) is faster for this specific test case, with an execution speed of 687073.5 per second, while `Array.from()` is significantly slower at 100589.6015625 per second. However, keep in mind that these results are specific to this particular test case and may not generalize to all scenarios. I hope this explanation helps!
Related benchmarks:
str split vs spread (LONG STRINGS) v1
Split join vs replace long string long string
Javascript: Case insensitive string comparison performance 3
String.replace(RegEx) vs String.replaceAll(String)
Comments
Confirm delete:
Do you really want to delete benchmark?