Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spread with mapper func
(version: 0)
Comparing performance of:
Array.from vs Spread
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Array.from
const double = v => v * 2; var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } var other = Array.from(fooSet, double);
Spread
const double = v => v * 2; var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } var other = [...fooSet].map(double);
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):
Let's dive into explaining the benchmark and its various aspects. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmarking test case, which measures the performance of two different approaches for converting a Set object to an array while applying a mapping function. The benchmark is designed to compare the performance of `Array.from` with spread operator (`...`) when used in conjunction with the `map()` method. **Options Compared** Two options are compared: 1. **`Array.from(fooSet, double)`**: This approach uses the `Array.from()` method to create a new array from the Set object, passing an iterator function that applies the `double` mapping function to each element. 2. **`[...fooSet].map(double)`**: This approach uses the spread operator (`...`) to convert the Set object to an array and then applies the `map()` method to transform each element. **Pros and Cons of Each Approach** 1. **`Array.from(fooSet, double)`**: * Pros: More explicit and readable syntax for creating arrays from iterables. * Cons: May incur additional overhead due to the creation of an iterator function. 2. **`[...fooSet].map(double)`**: * Pros: More concise and expressive syntax using spread operator and `map()`. * Cons: May have performance overhead due to array creation and `map()` method invocation. **Other Considerations** 1. **Library Usage**: The test case uses the Set object, which is a built-in JavaScript data structure. No external libraries are required. 2. **Special JS Features**: This benchmark does not utilize any special JavaScript features or syntax beyond the standard ECMAScript 2015+ specification. 3. **Alternative Approaches**: Other approaches to convert a Set object to an array might include using `Array.from()` with an array-like iterator (e.g., `[...fooSet.values()]`) or implementing a custom iterator function. **Benchmark Result** The latest benchmark result shows that the spread operator approach (`[...fooSet].map(double)`) outperforms the `Array.from` approach in terms of executions per second, with Chrome 113 executing approximately 164 k operations per second compared to around 117 k operations per second for `Array.from`.
Related benchmarks:
Array.from vs Spread with undefined and map
Splice vs Spread to insert at beginning of array
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array from vs Spread es6
toSpliced vs Spread
Comments
Confirm delete:
Do you really want to delete benchmark?