Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spread on arrays
(version: 0)
Comparing performance of:
Array.from vs Spread
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for(var i=0;i<100;i++) { arr.push(i); }
Tests:
Array.from
var other = Array.from(arr);
Spread
var other = [...arr];
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:
3 months 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
1222033.0 Ops/sec
Spread
1281122.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark and analyze the different approaches. **What is being tested?** The provided JSON represents a microbenchmark that compares two ways to create an array from another array: 1. Using `Array.from()` 2. Using the spread operator (`...`) In the script preparation code, an empty array `arr` is created and populated with 100 elements using a `for` loop. **Options compared** There are only two options being compared in this benchmark: 1. **`Array.from(arr)`**: This method creates a new array from the existing array `arr`. The method takes an iterable (in this case, `arr`) as its argument. 2. **`[...arr]`**: This is the spread operator, which also creates a new array from the elements of `arr`. **Pros and cons** Here are some pros and cons of each approach: 1. **`Array.from(arr)`** * Pros: + More explicit and readable syntax + Can be used with any iterable (not just arrays) + More flexible, as it can take an array or other iterables as arguments * Cons: + May incur additional overhead due to method call 2. **`[...arr]`** * Pros: + Concise and concise syntax + Often used in modern JavaScript code + Can be faster, since it's just an array expression with a spread operator * Cons: + Less explicit than `Array.from()` + May not work as expected if `arr` is not an array **Library usage** There are no libraries being used in this benchmark. **Special JavaScript feature or syntax** The spread operator (`[...arr]`) is a relatively modern JavaScript feature, introduced in ECMAScript 2015 (ES6). It's supported in most modern browsers and Node.js versions. **Other alternatives** If you want to compare other ways of creating an array from another array, here are some alternatives: * Using `concat()`: `arr.concat()` or `arr.concat([])` * Using `slice()`: `arr.slice(0)` (note that this will not work if the desired length is greater than the original array's length) * Using a for loop: `var newArr = []; for (var i = 0; i < arr.length; i++) { newArr.push(arr[i]); }` * Using `Array.prototype.map()`: `arr.map(function(x) { return x; })` Keep in mind that these alternatives may have different performance characteristics or syntax.
Related benchmarks:
Arrays: spread operator vs push
Array: spread operator vs push
Push vs Spread JavaScript
spread vs ArrayFrom
Comments
Confirm delete:
Do you really want to delete benchmark?