Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spread (small)
(version: 0)
Comparing performance of:
Array.from vs Spread
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Array.from
var fooSet = new Set(); for(var i=0;i<10;i++) { fooSet.add(i); } var other = Array.from(fooSet);
Spread
var fooSet = new Set(); for(var i=0;i<10;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:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.from
5072483.5 Ops/sec
Spread
5409313.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and the pros/cons of each approach. **Benchmark Overview** The benchmark compares two ways to convert a Set object to an Array: 1. `Array.from()` 2. Using the spread operator (`...`) **Script Preparation Code** The script preparation code is empty, which means that nothing specific needs to be done before running the tests. **HTML Preparation Code** The HTML preparation code is also empty, indicating that no special setup or configuration is required for the benchmark. **Individual Test Cases** There are two test cases: 1. **Array.from** * The benchmark definition uses `var fooSet = new Set();` to create a Set object and then iterates over it using a loop, adding each value to the set. * The test converts the Set object to an Array using `Array.from(fooSet);`. 2. **Spread** * This test is similar to the previous one, but uses the spread operator (`var other = [...fooSet];`) to convert the Set object to an Array. **What's Being Compared?** In both cases, we're measuring how many executions per second (ExecutionsPerSecond) are possible for each conversion method. **Options Compared** * `Array.from()` vs Spread Operator (`...`) + **Pros of Array.from():** - Explicit and clear syntax. - More widely supported across browsers. + **Cons of Array.from():** - May be slower due to the additional overhead of calling a function. - May have different performance characteristics depending on browser implementations. * Spread Operator (`...`): + **Pros:** - Faster execution, as it only involves a simple array assignment. - More concise syntax. + **Cons:** - Less explicit and may be less readable for some developers. - May have different performance characteristics depending on browser implementations. **Library Used** The benchmark uses the `Set` object, which is a built-in JavaScript data structure. The `Set` object provides an efficient way to store unique values and allows fast lookups, insertions, and deletions. **Special JS Feature or Syntax** Neither of the tested approaches relies on any special JavaScript features or syntax beyond what's available in modern browsers. However, it's worth noting that some older browsers might have different behavior or support for these APIs. **Other Alternatives** If you're interested in exploring alternative methods for converting a Set object to an Array, consider: * `Array.prototype.concat()`: This method concatenates all elements from the Set object into a new array. * `Array.from()` with a custom callback function: You can provide a callback function that processes each element in the Set object before adding it to the resulting array. Keep in mind that these alternatives might have different performance characteristics or may not be as efficient as using `Array.from()` or the spread operator.
Related benchmarks:
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array.from() vs spread []
Array.prototype.slice vs spread op
new Array using spreading operator vs Array.slice()
spread vs ArrayFrom
Comments
Confirm delete:
Do you really want to delete benchmark?