Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spread from array
(version: 0)
Comparing performance of:
Array.from vs Spread
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Array.from
var fooSet = []; for(var i=0;i<100;i++) { fooSet[i] = `${i}`; } var other = Array.from(fooSet);
Spread
var fooSet = []; for(var i=0;i<100;i++) { fooSet[i] = `${i}`; } var other = [...fooSet];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
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.from
2530152.2 Ops/sec
Spread
2942910.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and considered. **Benchmark Overview** The benchmark compares two approaches for converting an array to an array of strings using the spread operator (`[...]`) versus `Array.from()`. The test case creates an array `fooSet` with 100 elements, where each element is a string representation of its index (e.g., "0", "1", ..., "99"). It then measures the execution time for both approaches to convert this array to another array. **Options Compared** The two options being compared are: 1. **Array.from()**: A built-in JavaScript method that creates a new array from an iterable or array-like object. 2. **Spread operator (`[...]`)**: A syntax sugar for creating a new array by spreading elements from an existing array. **Pros and Cons** * **Array.from()**: + Pros: - More explicit and readable code - Can handle more complex conversions (e.g., converting an array of objects to an array of arrays) + Cons: - May have performance overhead due to the need for additional function calls * **Spread operator (`[...]`)**: + Pros: - Faster execution time, as it avoids the overhead of calling `Array.from()` - More concise and expressive code + Cons: - Less explicit and readable code (may be less intuitive for developers unfamiliar with this syntax) **Other Considerations** * **Library usage**: Neither test case uses any external libraries or dependencies. * **JS feature/syntax**: The spread operator is a relatively new JavaScript feature, introduced in ECMAScript 2015. It's widely supported across modern browsers and environments. **Benchmark Preparation Code** The benchmark preparation code creates an array `fooSet` with 100 elements using a simple `for` loop. This code is not specific to any particular library or dependency. **Individual Test Cases** Each test case measures the execution time for either the Array.from() approach (test case 1) or the spread operator approach (test case 2). The execution times are reported in the benchmark result section below. **Latest Benchmark Result** The latest benchmark result shows that the spread operator approach outperforms Array.from() by a small margin. This suggests that, on this particular test case, the spread operator is slightly faster due to its lower overhead. Keep in mind that performance differences between these two approaches may vary depending on specific use cases, system configurations, and hardware.
Related benchmarks:
Splice vs Spread to insert at beginning of array
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array.from() vs spread []
new Array using spreading operator vs Array.slice()
spread vs ArrayFrom
Comments
Confirm delete:
Do you really want to delete benchmark?