Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.from vs spread big data
(version: 0)
Comparing performance of:
array.from vs spread
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
array.from
var fooSet = new Set(); for(var i=0;i<1000;i++) { fooSet.add(i^2); } var other = Array.from(fooSet);
spread
var fooSet = new Set(); for(var i=0;i<1000;i++) { fooSet.add(i^2); } 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark measures the performance difference between two approaches: using `Array.from()` and using the spread operator (`...`) to convert a Set object to an array. **Tested Options** There are only two options being compared: 1. **`Array.from()`**: This method creates a new, shallow-copied Array instance from an iterable or an array-like object. 2. **Spread Operator (`...`)**: This operator is used to create a new array by spreading the elements of an array or an iterable. **Pros and Cons** * **`Array.from()`**: + Pros: Generally considered more efficient, as it creates a new array without modifying the original set. + Cons: May require additional memory allocation, depending on the size of the set. * **Spread Operator (`...`)**: + Pros: Can be faster for small to medium-sized sets, as it uses a more lightweight approach that doesn't create a new array. + Cons: Creates a new array and can lead to unexpected behavior if not used carefully. **Library and Special JS Features** In this benchmark, the following libraries are not explicitly mentioned: * No external libraries are required for this benchmark. * There is no special JavaScript feature or syntax being tested. **Benchmark Preparation Code** The script preparation code for each test case is: ```javascript var fooSet = new Set(); for (var i = 0; i < 1000; i++) { fooSet.add(i ^ 2); } ``` This code creates a new Set object `fooSet` and adds the result of the expression `i ^ 2` to it, using a loop that runs 1000 times. **Individual Test Cases** There are two test cases: 1. **`array.from`**: This test case uses the `Array.from()` method to convert the `fooSet` object to an array. ```javascript var other = Array.from(fooSet); ``` 2. **`spread`**: This test case uses the spread operator (`...`) to convert the `fooSet` object to an array. ```javascript var other = [...fooSet]; ``` **Latest Benchmark Result** The latest benchmark result shows that the spread operator (`...`) outperforms `Array.from()` on this specific test case, with approximately 12.5% fewer executions per second. Here is a summary of the results: | Test Name | Executions Per Second | | --- | --- | | Spread | 18715.61328125 | | Array.from | 18407.51171875 | **Other Alternatives** If you want to explore other approaches, here are some alternatives: * **`Set.prototype.values()`**: This method returns an iterator over the values of a Set object. * **`Array.prototype.slice()`**: This method creates a shallow copy of a portion of an array. * **`Array.prototype.concat()`**: This method concatenates one or more arrays. Keep in mind that each approach has its own trade-offs and may be more suitable for specific use cases.
Related benchmarks:
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array.push vs Spread operator
Array.from() vs spread []
spread vs ArrayFrom
Array.slice() vs. Spread operator (10000 items)
Comments
Confirm delete:
Do you really want to delete benchmark?