Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs spread for converting set to array
(version: 0)
Comparing performance of:
Array.from vs spread
Created:
5 years ago
by:
Guest
Go to the latest result
Tests:
Array.from
const fooSet = new Set(); for(let i=0;i<2000;i++){ fooSet.add(i); } const arrayFromSet = Array.from(fooSet)
spread
const fooSet = new Set(); for(let i=0;i<2000;i++){ fooSet.add(i); } const arrayFromSet = [...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 year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
spread
38K/s
Array.from
38K/s
View exact numbers
Test name
Executions per second
✓
spread
38,170 Ops/sec
Array.from
38,125 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Definition** The benchmark is comparing two ways to convert a Set object to an Array: 1. Using the `Array.from()` method 2. Using the spread operator (`...`) **Options Compared** We have two options being compared: * Option 1: `Array.from(fooSet)` + This method creates a new array from the elements of the Set object. + It's a concise and expressive way to convert a Set to an Array. + However, it may incur overhead due to the creation of a new array. * Option 2: `fooSet` + By using the spread operator (`...`) directly on the Set object, we're spreading its elements into a new array. + This approach avoids creating a new array and instead uses the existing Set's internal data structure as an intermediate step. + However, it may be less readable and not suitable for all use cases. **Pros and Cons** * `Array.from()`: + Pros: concise, expressive, and easy to read. + Cons: may incur overhead due to array creation. * Spread operator (`...`): + Pros: avoids array creation, potentially more efficient. + Cons: less readable, not suitable for all use cases. **Library** There is no library being used in this benchmark. However, the `Set` object and its methods are part of the JavaScript standard library. **Special JS Feature or Syntax** None mentioned. The benchmark only uses standard JavaScript features and syntax. **Alternative Approaches** Other ways to convert a Set to an Array could include: * Using the `Array.prototype.forEach()` method: `const array = []; fooSet.forEach((element) => { array.push(element); });` * Using a custom implementation, such as using a buffer or array-like object to store elements. Keep in mind that these alternative approaches might be less efficient and more complex than the options being compared in this benchmark.
Related benchmarks
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array.from() vs spread []
Comments
Confirm delete:
Do you really want to delete benchmark?