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?`;
let n1 = Array.from(s1);
let n2 = Array.from(s2);
let n3 = Array.from(s3);
let n1 = s1.split('');
let n2 = s2.split('');
let n3 = s3.split('');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.from | |
string split |
Test name | Executions per second |
---|---|
Array.from | 100589.6 Ops/sec |
string split | 687073.5 Ops/sec |
This benchmark compares two ways to split a string into an array of characters in JavaScript:
1. Array.from()
: This method iterates over the string and creates an array with each character as its own element. It's considered more modern and readable, especially for strings.
2. .split('')
: This is a built-in string method that splits the string into an array of substrings using a specified separator (in this case, an empty string ''
).
Benchmark Description:
The benchmark tests these methods on three different strings:
s1
: A short string "foo".s2
: A moderately sized string "foo 12312".s3
: A very long string containing a section from Cicero's "De Finibus Bonorum et Malorum" and a translation by H. Rackham.Pros/Cons:
Array.from()
.split('')
due to the additional iteration step in certain JavaScript engines..split('')
Array.from()
because it's a built-in string method optimized by the browser/JavaScript engine.Other Considerations:
.split('')
is significantly faster than Array.from()
on average, especially with larger strings. This difference in performance highlights how optimizations within built-in string methods often outweigh the potential readability benefits of Array.from()
.Alternatives:
While not directly comparable to these options, other ways to create arrays from strings exist:
new RegExp().exec()
): Can be used to split strings based on patterns rather than just individual characters. More complex but powerful for specific string manipulations.for
loops: Manually iterating over each character in a string and creating an array element by element. Less efficient and more verbose than the methods tested here, typically avoided unless there are specific performance or control requirements.