Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spread isolated
(version: 0)
fork to test Array.from vs spread exclusive of setup code
Comparing performance of:
Array.from vs Spread
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); }
Tests:
Array.from
var other = Array.from(fooSet);
Spread
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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.from
2366326.8 Ops/sec
Spread
3190077.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **What is being tested?** The benchmark tests two different approaches to create an array from a Set object: `Array.from` and the spread operator (`...`). The test is designed to isolate the impact of each approach on performance, by excluding setup code. **Options compared:** 1. **`Array.from()`**: This method creates a new array from an iterable or an array-like object. In this case, it's used to create an array from a Set object. 2. **Spread operator (`...`)**: This operator can be used to convert an iterable or an array-like object into an array. **Pros and Cons of each approach:** 1. **`Array.from()`**: * Pros: + More explicit and readable way to create an array from a Set. + Can be more efficient in some cases, since it avoids creating a temporary array. * Cons: + May have additional overhead due to the function call and argument processing. 2. **Spread operator (`...`)**: * Pros: + More concise and expressive way to create an array from a Set. + Can be faster, since it avoids creating a temporary array. * Cons: + May have additional overhead due to the parsing of the spread expression. **Library used:** In this benchmark, the `Set` object is used, which is a built-in JavaScript object that implements the Set interface. The purpose of the Set object is to store unique values and maintain their insertion order. **Special JS feature or syntax:** The benchmark uses the spread operator (`...`) to convert the Set object into an array. This feature was introduced in ECMAScript 2015 (ES6) as part of the Array.prototype spread syntax. **Other alternatives:** In addition to `Array.from()` and the spread operator, there are other ways to create an array from a Set object, such as: * Using `set.values()`: This method returns an iterator over the values in the Set object. * Using `set.keys()`: This method returns an iterator over the keys (i.e., unique values) in the Set object. * Converting the Set object to an array using other methods, such as `JSON.stringify()` or `Array.from([].concat(set))`. However, these alternatives may have additional overhead or complexity compared to the benchmark's simple approach. Now that we've broken down the benchmark, it should be clear what's being tested and why.
Related benchmarks:
JavaScript spread operator vs Object.assign performance (fixed)
JavaScript spread operator vs Object.assign (no mutate) performance
Fork of JavaScript spread operator vs Object.assign performance
Object.assign() vs spread operator (New object)
Array.from vs Spread using 10000 elements
Comments
Confirm delete:
Do you really want to delete benchmark?