Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs forEach
(version: 0)
Comparing performance of:
Array.from vs Spread
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
Array.from
var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } var other = Array.from(fooSet);
Spread
var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } const other = []; fooSet.forEach(value => other.push(value));
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 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 131 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.from
153011.6 Ops/sec
Spread
116446.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **What is being tested?** The provided benchmark compares two ways to convert a Set object into an array: using `Array.from()` and using the spread operator (`...`). The benchmark measures which approach is faster on a specific test case, where a Set is created with 100 elements and then converted into an array. **Options compared:** * `Array.from()`: This method creates a new array from an iterable or an array-like object. It's a more explicit way to convert between sets and arrays. * Spread operator (`...`): This operator allows you to expand an array-like object into separate arguments for a function, but it can also be used as a shorthand to create a new array from an existing one. **Pros and cons of each approach:** * `Array.from()`: Pros: + More explicit and easier to understand. + Allows for more control over the conversion process (e.g., selecting specific elements). Cons: + May be slower due to the overhead of creating a new array. * Spread operator (`...`): Pros: + Faster, since it doesn't create a new array explicitly. + Can be used as a shorthand to convert arrays into other data structures. Cons: + Less explicit and more prone to errors (e.g., incorrect usage). + May not work well with older browsers or environments. **Library/technology involved:** In the test case, `Set` is being used. A Set is a collection of unique values, ordered in no particular way. It's similar to an array, but without the duplicate elements. In this benchmark, the `Set` object is created and then converted into an array using both methods. **Special JS feature/syntax:** There are no special JavaScript features or syntax mentioned in this benchmark. Both `Array.from()` and the spread operator are standard JavaScript features that have been around for a while. **Other alternatives:** If you're looking for alternative ways to convert sets into arrays, you could consider using other methods like: * Using `reduce()`: This method applies a reduction function to each element in an array (or set) and returns the final value. * Using `map()`: This method creates a new array by applying a given function to each element of an existing array (or set). However, these alternatives are not used in this benchmark, and their performance may vary depending on the specific use case. Overall, the benchmark provides a good comparison between two common methods for converting sets into arrays, allowing developers to choose the most suitable approach based on performance and readability requirements.
Related benchmarks:
Iteration through array; of vs forEach
map vs forEach Chris v2b
foreach vs for..of
foreach vs for...of
forEach vs for of 7
Comments
Confirm delete:
Do you really want to delete benchmark?