Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Array.from vs Spread vs arrayPrototypeSlice
(version: 0)
Benchmark.js
Comparing performance of:
Array.from vs Spread vs array.prototype.slice.call
Created:
4 years ago
by:
Guest
Go to the latest result
Tests:
Array.from
var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } var other = Array.from(fooSet);
Spread
var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } var other = [...fooSet];
array.prototype.slice.call
var fooSet = new Set(); for(var i=0;i<100;i++) { fooSet.add(i); } var other = Array.prototype.slice.call(fooSet);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.from
Spread
array.prototype.slice.call
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0
Browser/OS:
Firefox 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
array.prototype.slice.call
126K/s
Array.from
108K/s
Spread
105K/s
View exact numbers
Test name
Executions per second
✓
array.prototype.slice.call
126,136 Ops/sec
Array.from
108,050 Ops/sec
Spread
104,776 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark and explain what is being tested, compared, and the pros/cons of each approach. **Benchmark Overview** The benchmark compares three methods for converting a Set to an array: 1. `Array.from()` 2. Spread syntax (`[...fooSet]`) 3. `arrayPrototypeSlice.call()` These methods are used to iterate over the elements of a Set and create a new array from it. **Options Compared** The options being compared are: * `Array.from()`: a modern method for converting an iterable (like a Set) to an array. * Spread syntax (`[...fooSet]`): a shorthand way of creating an array by spreading the elements of another iterable (in this case, a Set). * `arrayPrototypeSlice.call()`: a legacy method that uses the `slice()` function on the Array prototype and passes the Set as the first argument. **Pros/Cons of Each Approach** 1. **`Array.from()`**: * Pros: modern, efficient, and widely supported. * Cons: not available in older browsers or JavaScript versions (before ECMAScript 2015). 2. **Spread Syntax (`[...fooSet]`)**: * Pros: concise, readable, and efficient. * Cons: might be slower than `Array.from()` due to the overhead of creating a new array. 3. **`arrayPrototypeSlice.call()`**: * Pros: widely supported, but might be less readable and more verbose. * Cons: older browsers or JavaScript versions might not support this method. **Library Used** None explicitly mentioned in the provided benchmark JSON. However, it's worth noting that `Array.from()` was introduced in ECMAScript 2015 (ES6), which is a modern standard. **Special JS Feature/Syntax** The spread syntax (`[...fooSet]`) uses the "rest parameter" feature of JavaScript, which was also introduced in ES6. This allows us to create new arrays by spreading the elements of another iterable. **Other Alternatives** In addition to the three methods being compared in this benchmark, other alternatives might include: * Using a traditional `for` loop to iterate over the Set and push elements into an array. * Using a library like Lodash or Ramda for array creation. Keep in mind that these alternative approaches might not be as efficient or readable as the three methods being compared.
Related benchmarks
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array .push() vs .unshift() vs spread
Array.push vs Spread operator
Array.from() vs spread []
Comments
Confirm delete:
Do you really want to delete benchmark?