Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spread using 10000 elements
(version: 0)
Comparing performance of:
Array.from vs Spread
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Array.from
var fooSet = new Set(); for(var i=0;i<10000;i++) { fooSet.add(i); } var other = Array.from(fooSet);
Spread
var fooSet = new Set(); for(var i=0;i<10000;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:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.from
1434.3 Ops/sec
Spread
1396.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what is being tested. **Benchmark Definition** The benchmark is testing two approaches to convert a Set object to an Array: `Array.from` and using the spread operator (`...`). The test case uses 10,000 elements. **Options Compared** Two options are compared: 1. **Array.from**: This method creates a new Array instance from an iterable or an array-like object. It's specifically designed for converting Sets, Arrays, and other iterable objects to arrays. 2. **Spread Operator (`...`)**: This operator can be used to convert a Set to an array by spreading its elements into a new array. **Pros and Cons** * **Array.from**: Pros: + More explicit and readable way to convert Sets to arrays. + Can handle larger sets without performance issues. Cons: + May have slightly slower execution times compared to the spread operator. * **Spread Operator (`...`)**: Pros: + Often faster than `Array.from`. + More concise and easier to read in some cases. Cons: + Less explicit and may be less readable for developers unfamiliar with this syntax. **Library and Purpose** None of the test cases explicitly use a library, but they do rely on built-in JavaScript features: * `Set`: A data structure that stores unique values. * `Array.from()`: A method that converts an iterable to an array. * Spread Operator (`...`): A feature introduced in ECMAScript 2015 (ES6) that allows elements to be spread into a new array or object. **Special JS Feature** The test case uses the spread operator, which is a special JavaScript feature. However, since it's already part of the standard library since ES6, no additional explanation is needed. **Other Alternatives** There are other ways to convert Sets to arrays, such as using `Array.prototype.slice()` or creating a custom function. However, these approaches may not be as efficient or readable as `Array.from` or the spread operator. If you're interested in exploring alternative approaches, here's an example of using `Array.prototype.slice()`: ```javascript var fooSet = new Set(); for (var i = 0; i < 10000; i++) { fooSet.add(i); } var other = Array.prototype.slice.call(fooSet); ``` Keep in mind that this approach may have performance implications due to the overhead of calling `slice()` on an array.
Related benchmarks:
Array.prototype.slice vs spread operator with length limit
Push vs LHS spread
Spread vs Slice operators in JS
Array.from vs Spread using 1000000 elements / only counts conversion
Array.from vs Spread using 10000 elements / only counts conversion
Comments
Confirm delete:
Do you really want to delete benchmark?