Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
str split vs spread vs Array.from
(version: 1)
Comparing performance of:
split vs spread vs array.from
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = "hello";
Tests:
split
var other = str.split('');
spread
var other = [...str];
array.from
var other = Array.from(str)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
split
spread
array.from
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:141.0) Gecko/20100101 Firefox/141.0
Browser/OS:
Firefox 141 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
split
57030644.0 Ops/sec
spread
43860648.0 Ops/sec
array.from
25493302.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON compares three different methods for splitting a string into an array of individual characters in JavaScript. The methods being compared are: 1. **String.prototype.split()** 2. **Spread syntax ([...string])** 3. **Array.from()** ### Test Cases and Methods 1. **String.prototype.split()** - **Benchmark Definition**: `var other = str.split('');` - **Description**: This method takes a separator (in this case, an empty string `''`) and splits the string into an array of substrings. Since an empty string is used, it splits the string at every character. - **Pros**: - Well-established and widely used. - Provides the option to limit the number of splits with a second parameter. - **Cons**: - Can be slower for simple character splitting compared to more modern methods. - Less readable when used for this specific purpose. 2. **Spread Syntax** - **Benchmark Definition**: `var other = [...str];` - **Description**: The spread syntax expands the string into an iterable collection, creating a new array with each character as a separate element. - **Pros**: - Concise and readable syntax, making it easy to understand. - Directly converts the string into an array format. - **Cons**: - A relatively newer feature; may not be supported in all older environments. 3. **Array.from()** - **Benchmark Definition**: `var other = Array.from(str);` - **Description**: This method creates a new array from an array-like or iterable object, in this case, a string. It effectively captures each character as an element in an array. - **Pros**: - Flexible and can take a mapping function as a second argument. - Works well with other iterable objects. - **Cons**: - Slightly less intuitive than spread syntax for simple use cases. ### Benchmark Results The results indicate the number of executions per second for each method tested in Chrome 135 on a Windows desktop: - **`split`**: 63,589,000 executions per second - **`spread`**: 44,361,132 executions per second - **`array.from`**: 43,460,540 executions per second ### Considerations and Alternatives 1. **Performance**: - The `split` method outperformed the other two approaches in this benchmark, highlighting that for simple string manipulations, it remains very efficient and effective. - When choosing between these methods, performance may be a critical factor, especially when dealing with larger strings or in performance-sensitive applications. 2. **Readability**: - The spread syntax is often preferred for its clarity and succinctness in code, which can improve maintainability. 3. **Environment Support**: - Consideration of browser support is vital. While `split` has universal support, the spread syntax and `Array.from()` may not support older browsers (e.g., Internet Explorer). 4. **Alternative Methods**: - Other alternatives include using classic loops or the `map` function on a string converted to an array using `split` if transformation of characters is required. - Regular expressions with the `match` method can also be used to achieve similar outcomes in more complex string manipulations. Overall, the choice between these approaches should be made based on the context, considering factors such as performance, readability, browser support, and specific needs of the application. Each method has its place in the toolbox of a JavaScript developer.
Related benchmarks:
Compare string.split to a spread opperator
str split vs spread
Array.from vs Spread for strings
JSON.parse vs string.split small fixed array
JSON.parse vs string.split 2
string split vs spread vs array from
array.split vs array.from vs spread
str.split vs spread
JSON.parse vs string.splitds
Comments
Confirm delete:
Do you really want to delete benchmark?