Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.from vs spread syntax
(version: 0)
Comparing performance of:
array.from vs spread syntax
Created:
6 years ago
by:
Registered User
Jump to the latest result
Tests:
array.from
var map = new Map(); map.set("hello", "world"); map.set("done", "do"); const array = Array.from(map.values());
spread syntax
var map = new Map(); map.set("hello", "world"); map.set("done", "do"); const array = [...map.values()]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array.from
spread syntax
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 tests the performance of two ways to create an array from the values of a Map object in JavaScript: **1. `Array.from()` method:** This method takes an iterable (like a Map's `.values()`) and creates a new array with its elements. The code for this test case is: ```javascript var map = new Map(); map.set("hello", "world"); map.set("done", "do"); const array = Array.from(map.values()); ``` **2. Spread syntax (`...`):** This syntax expands the values of an iterable directly into a new array. The code for this test case is: ```javascript var map = new Map(); map.set("hello", "world"); map.set("done", "do"); const array = [...map.values()]; ``` **Pros and Cons:** * **`Array.from()`**: * **Pros:** Well-established method, generally considered safe and reliable. * **Cons:** Can be slightly slower than the spread syntax in some cases due to the extra overhead of a separate method call. * **Spread Syntax (`...`)**: * **Pros:** More concise and often faster than `Array.from()`. * **Cons:** Newer syntax (ES6+), may not be as widely supported in older browsers, can sometimes lead to less readable code depending on context. **Other Considerations:** * **Context Matters:** In most cases, the performance difference between these two approaches will be negligible unless you're dealing with extremely large Maps or running this operation millions of times per second. * **Readability:** If you prioritize code readability, `Array.from()` might be slightly easier to understand for those unfamiliar with the spread syntax. **Alternatives:** There are other ways to iterate over a Map's values and potentially create an array: * **`for...of` loop:** This provides more explicit control over the iteration process but might require more code. ```javascript const array = []; for (const value of map.values()) { array.push(value); } ``` * **`map()` method:** You can use `map()` on an iterable to transform each element, effectively creating a new array with modified values: ```javascript const array = Array.from(map.values()).map(value => value.toUpperCase()); ```
Related benchmarks:
Array.from vs Spread with undefined and map
Splice vs Spread to insert at beginning of array
Javascript string to array mapping: Array.from() vs Spread syntax [...spread]
Array.from() vs spread []
spread vs ArrayFrom
Comments
Confirm delete:
Do you really want to delete benchmark?