Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.split vs array.from vs spread
(version: 0)
Comparing performance of:
Array.split vs Array.from vs Spread
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var s1 = "foobar"; var s2 = "foo";
Tests:
Array.split
var n1 = s1.split(""); var n2 = s2.split("");
Array.from
var n1 = Array.from(s1); var n2 = Array.from(s2);
Spread
var n1 = [...s1]; var n2 = [...s2];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.split
Array.from
Spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.split
36701380.0 Ops/sec
Array.from
23116464.0 Ops/sec
Spread
23342948.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases to understand what is being tested. **Benchmark Definition** The benchmark definition represents a set of microbenchmarks that compare three different approaches to manipulate arrays in JavaScript: 1. `Array.split()` 2. `Array.from()` 3. The spread operator (`...`) **Script Preparation Code** The script preparation code provides the input strings used for each test case: ```javascript var s1 = "foobar"; var s2 = "foo"; ``` These strings are likely used as input for the array manipulation functions being compared. **Options Compared** The benchmark compares three options: 1. `Array.split()` with a specified separator (`"\ "`). 2. `Array.from()` with an empty array as the initial value. 3. The spread operator (`...`) to create a new array from an existing one. **Pros and Cons of Each Approach** Here's a brief overview of each approach, including their pros and cons: 1. **`Array.split()`** * Pros: Simple, widely supported, and efficient for splitting strings into arrays. * Cons: Can be less efficient than other approaches when dealing with large arrays or complex separators. 2. **`Array.from()`** * Pros: Flexible, allows for creating an array from any iterable, and can be more efficient for large datasets. * Cons: May incur a higher overhead due to the creation of a new array object. 3. **Spread Operator (`...`)** * Pros: Simple, intuitive, and often faster than `Array.from()` for small arrays. * Cons: Can be less flexible than `Array.from()`, as it only works with arrays. **Library Usage** In this benchmark, no external libraries are used beyond the built-in JavaScript functionality. The `Array.from()` method is a native JavaScript function that creates a new array from an iterable (in this case, the input string). **Special JS Feature or Syntax** The benchmark does not use any special JavaScript features or syntax beyond what's commonly used in modern JavaScript development. **Other Alternatives** If you're interested in exploring alternative approaches to manipulating arrays in JavaScript, here are some additional methods: 1. `Array.prototype.slice()`: Similar to `Array.split()`, but with more control over the slice position. 2. `String.prototype.split()` with a specified separator: Can be used as an alternative to `Array.split()`. 3. `Array.prototype.concat()` or `Array.prototype.push()`: Can be used for more complex array manipulation scenarios. Keep in mind that these alternatives might not provide identical performance characteristics or features compared to the options tested in this benchmark.
Related benchmarks:
Array.prototype.slice Versus Spread Operator
Which is faster Array.prototype.slice vs spread operator
string split vs spread vs array from
Array from vs string split vs spread
Array.prototype.slice vs spread operator new
Comments
Confirm delete:
Do you really want to delete benchmark?