Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spreadasdf
(version: 0)
Comparing performance of:
Array.from vs Spread
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Array.from
var fooSet = new Set(); for(var i=0;i<100000;i++) { fooSet.add(i); } var other = Array.from(fooSet);
Spread
var fooSet = new Set(); for(var i=0;i<100000;i++) { fooSet.add(i); } 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/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.from
254.8 Ops/sec
Spread
242.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Benchmark Overview** The provided benchmark measures the performance difference between using `Array.from()` and the spread operator (`...`) to convert a JavaScript `Set` object to an array. The benchmark is designed to test the efficiency of these two approaches in creating an array from a set with a large number of elements (100,000). **Options Being Compared** Two options are being compared: 1. **Array.from()**: This method creates a new array from an iterable or any sequence. In this case, it's used to convert the `Set` object `fooSet` to an array. 2. **Spread Operator (`...`)**: This operator is used to create a new array by spreading elements of an iterable. **Pros and Cons of Each Approach** * **Array.from()**: + Pros: - More explicit and readable code - Can handle any type of iterable, not just sets - Can be more efficient for large datasets due to optimization in modern browsers + Cons: - May require additional memory allocation - Can be slower than the spread operator for very large datasets * **Spread Operator (`...`)**: + Pros: - More concise and expressive code - Can be faster than `Array.from()` for large datasets due to optimized implementation in modern browsers - Requires less memory allocation compared to `Array.from()` + Cons: - Less explicit and readable code, which can lead to errors if not used carefully - May not work correctly with all types of iterables **Other Considerations** * **Browser Support**: Both approaches are widely supported by modern browsers. However, it's essential to note that older browsers may have varying levels of support for these features. * **JavaScript Version**: The benchmark assumes a relatively recent version of JavaScript (likely ECMAScript 2015 or later). If using an earlier version, the spread operator might not be available. **Library and Special JS Features** Neither `Array.from()` nor the spread operator relies on any specific libraries. However, they do utilize built-in JavaScript features that are part of the language standard. **Test Case Analysis** The test cases compare the execution times of creating an array from a set using both approaches. The benchmark result indicates that: * **Array.from()** outperforms the spread operator for this particular use case. * The execution times are relatively close, suggesting that the difference is negligible for small to medium-sized datasets. **Alternatives** If you're looking for alternative approaches, consider: * Using `Set.prototype.values()` or `Set.prototype.entries()` instead of `Array.from()`. * Utilizing the `map()` method with a callback function to convert the set to an array. * Employing other iterative methods, like using loops and `push()` or `splice()` to create an array. Keep in mind that these alternatives might not offer significant performance improvements for this specific use case. However, they can provide more flexibility and control over the conversion process.
Related benchmarks:
toFixed() vs Math.round().toString()
toFixed() vs String(Math.floor()
toFixed() vs String(Math.floor()) vs Math.floor().toString()
parseFloat(toFixed) vs Math.round()
Array.from vs Spread using 10000 elements
Comments
Confirm delete:
Do you really want to delete benchmark?