Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spread - performance benchmark
(version: 0)
Comparing performance of:
Array.from - Set vs Spread - Set vs Array.from - Array vs Spread - Array
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Array.from - Set
var fooSet = new Set(); for(var i=0;i<1000000;i++) { fooSet.add(i); } var other = Array.from(fooSet);
Spread - Set
var fooSet = new Set(); for(var i=0;i<1000000;i++) { fooSet.add(i); } var other = [...fooSet];
Array.from - Array
var fooArr = []; for(var i=0;i<1000000;i++) { fooArr.push(i); } var other = Array.from(fooArr);
Spread - Array
var fooArr = []; for(var i=0;i<1000000;i++) { fooArr.push(i); } var other = [...fooArr];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array.from - Set
Spread - Set
Array.from - Array
Spread - Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the details of this benchmark. **What is being tested?** The test case compares the performance of two methods to create an array from a collection of elements: 1. `Array.from()`: This method takes an iterable (such as an array, set, or map) and returns a new array containing all elements. 2. Spread syntax (`[...array]`): This operator unpacks an iterable into individual elements. **What options are being compared?** The test cases compare the performance of these two methods across two types of input collections: 1. `Set`: A set is an unordered collection of unique values. 2. `Array`: An array is a ordered collection of values. **Pros and Cons:** * **Array.from()**: + Pros: - More explicit and readable code - Can handle any iterable, not just arrays + Cons: - May be slower than spread syntax for small inputs (due to overhead of function call) * **Spread syntax (`[...array]`)**: + Pros: - Faster than `Array.from()` for small inputs (due to operator overloading) - More concise and expressive code + Cons: - Only works with iterables, not arbitrary objects **Other considerations:** * The test case uses a large input set (`1000000` elements) to simulate a realistic scenario. * The performance difference between `Array.from()` and spread syntax may be significant for small inputs but becomes negligible for larger inputs. **Library usage:** None in this specific benchmark. However, if you're familiar with JavaScript testing libraries like Jest or Mocha, you might expect to see a library used here to create the test environment and run the benchmarks. **Special JS feature or syntax:** The spread syntax (`[...array]`) is a special operator in JavaScript that unpacks an iterable into individual elements. It's not a new language feature but rather a concise way to achieve a common operation. **Alternatives:** Other alternatives for creating an array from a collection could be: 1. Using the `map()` function to create a new array by mapping each element of the input collection. 2. Using the `forEach()` method to iterate over the input collection and manually add elements to a new array. 3. Using a library-specific method, such as Lodash's `from()`, to achieve similar functionality. However, these alternatives are not being compared in this specific benchmark. The focus is on comparing the performance of `Array.from()` and spread syntax.
Related benchmarks:
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array.push vs Spread operator
Push vs LHS spread
Array.from() vs spread []
Array.slice() vs. Spread operator (10000 items)
Comments
Confirm delete:
Do you really want to delete benchmark?