Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spreadv2
(version: 1)
Comparing performance of:
Array.from vs Spread
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var fooSet = new Set(); for(var i=0;i<10000;i++) { fooSet.add(i); }
Tests:
Array.from
var other = Array.from(fooSet);
Spread
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:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.from
75371.4 Ops/sec
Spread
76018.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark measures the performance difference between two approaches to create an array from a set: `Array.from` (introduced in ECMAScript 2015) and the spread operator (`...`). The script preparation code creates a set of 10,000 integers using `Set`, and then attempts to convert it into an array using each of the two methods. **Options Compared** 1. **`Array.from`**: Creates an array from an iterable (in this case, a set) by iterating over its elements. 2. **Spread Operator (`...`)**: Creates an array from an iterable by spreading its elements. **Pros and Cons of Each Approach** 1. **`Array.from`**: * Pros: + More explicit and readable code + Can be more efficient in certain cases, as it allows for custom mapping functions * Cons: + May incur additional overhead due to the iteration over the set's elements 2. **Spread Operator (`...`)** * Pros: + More concise and expressive code + Often faster, as it avoids the iteration step * Cons: + Less explicit and potentially less readable code + May not be compatible with all browsers or versions **Library and Special JS Features** Neither `Array.from` nor the spread operator relies on any specific libraries. However, `Array.from` does support custom mapping functions, which can be used to transform the elements of the set before creating the array. **Test Case Considerations** The test case uses a simple set of 10,000 integers to simulate a large dataset. This allows for a straightforward comparison between the two approaches. The use of `Set` and arrays ensures that the benchmark is measuring the performance difference in these specific data structures. **Other Alternatives** If you were to rewrite this benchmark using alternative methods, you might consider: 1. Using a custom array creation function, such as `new Int32Array(10000)`, which could provide additional insight into the performance characteristics of different array creation methods. 2. Utilizing other spread operators, like `JSON.parse(JSON.stringify(fooSet))`, to explore the performance implications of spreading objects versus arrays. 3. Measuring the performance of alternative set-to-array conversions, such as using `Array.prototype.concat()` or `Array.prototype.push()`, which might provide interesting insights into the trade-offs between different approaches. Keep in mind that these alternatives would require significant changes to the benchmark definition and script preparation code.
Related benchmarks:
JavaScript spread operator vs Object.assign (no mutate) performance
JavaScript spread operator vs Object.assign performance with overwite
JavaScript spread operator vs Object.assign performance(New Reference)
JavaScript spread operator vs Object.assign performance v3455676854743
Array.from vs Spread using 10000 elements
Comments
Confirm delete:
Do you really want to delete benchmark?