Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spread - performance benchmark
(version: 0)
Comparing performance of:
Array.from - Set vs Spread - Set vs Array.from - Array vs Spread - Array
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Array.from - Set
var fooSet = new Set(); for(var i=0;i<1000000;i++) { fooSet.add(i); } var other = Array.from(fooSet);
Spread - Set
var fooSet = new Set(); for(var i=0;i<1000000;i++) { fooSet.add(i); } var other = [...fooSet];
Array.from - Array
var fooArr = []; for(var i=0;i<1000000;i++) { fooArr.push(i); } var other = Array.from(fooArr);
Spread - Array
var fooArr = []; for(var i=0;i<1000000;i++) { fooArr.push(i); } var other = [...fooArr];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Array.from - Set
Spread - Set
Array.from - Array
Spread - Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares the performance of two ways to create arrays from Sets and Arrays in JavaScript: **Method 1: `Array.from()`** * **Description:** This method takes an iterable object (like a Set or Array) and returns a new array containing its elements. **Method 2: Spread Operator (`...`)** * **Description:** The spread operator, when used with iterable objects, expands their contents into individual items. This can be directly used within the square brackets of an array literal to create a new array. **Code Examples:** ```javascript // Array.from method var other = Array.from(fooSet); // fooSet is a Set object // Spread operator var other = [...fooSet]; // fooSet is a Set object ``` **Pros and Cons:** * **`Array.from()`** * **Pros:** Widely supported across different JavaScript engines, well-documented, readable syntax. * **Cons:** Can potentially be slightly slower than the spread operator in some cases due to its need for more complex internal operations. * **Spread Operator (`...`)** * **Pros:** Often faster than `Array.from()` due to being a lower-level operation that leverages JavaScript's efficient iteration mechanisms. Concise and modern syntax. * **Cons:** Introduced in ES6, might not be supported by older browsers requiring polyfills for compatibility. **Other Considerations:** * **Data Structures:** While this benchmark focuses on Sets and Arrays, the performance differences between `Array.from()` and the spread operator can also apply to other iterable data structures like strings or maps. **Alternatives:** 1. **`forEach()` Loop:** - You could manually loop through the Set or Array using a `forEach()` loop and build the new array element by element. This offers fine-grained control but is generally less efficient for performance. Let me know if you'd like to explore any of these points in more detail!
Related benchmarks:
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array.push vs Spread operator
Push vs LHS spread
Array.from() vs spread []
Array.slice() vs. Spread operator (10000 items)
Comments
Confirm delete:
Do you really want to delete benchmark?