Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spread 10000 elements + addition of element
(version: 0)
Comparing performance of:
Array.from vs Spread
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Array.from
var fooSet = new Set(); for(var i=0;i<10000;i++) { fooSet.add(i); } var other = Array.from(fooSet); other.push(1000)
Spread
var fooSet = new Set(); for(var i=0;i<10000;i++) { fooSet.add(i); } var other = [...fooSet, 1000];
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **What is being tested?** The provided JSON represents two individual test cases for measuring performance differences between `Array.from` and the spread operator (`...`) when: 1. Creating a Set with 10,000 elements and then converting it to an array using `Array.from`. 2. Adding an element to the end of the resulting array. **Options being compared:** The two options being compared are: 1. **`Array.from()`**: A method that creates a new array from an iterable (such as a Set) by copying all elements into the new array. 2. **Spread operator (`...`)**: An operator that converts an iterable to an array by creating a new array with the given elements. **Pros and Cons of each approach:** 1. `Array.from()`: * Pros: + More explicit and readable code, as it's a method call that clearly indicates the intention. + May be faster for certain use cases, since it avoids the overhead of operator overloading. * Cons: + Requires an additional method call, which can incur a small performance penalty. 2. Spread operator (`...`): * Pros: + More concise and readable code, as it's a single operator that clearly conveys the intention. + May be faster for certain use cases, since it avoids the overhead of a method call. * Cons: + Requires knowledge of the spread operator syntax, which can be unfamiliar to some developers. **Library usage:** In both test cases, the Set data structure is used. A Set is a collection of unique values that cannot be duplicated. In JavaScript, Sets are implemented as hash tables, which provide fast lookup and insertion times. **Special JS feature or syntax:** The spread operator (`...`) is a relatively recent addition to JavaScript (introduced in ES6) and provides a concise way to create arrays from iterables. **Benchmark results:** According to the latest benchmark result, Chrome 118 outperforms `Array.from()` with an execution rate of 3187.558349609375 executions per second, compared to 3181.62060546875 for `Array.from()`. This suggests that, in this specific test case, the spread operator may be slightly faster. **Other alternatives:** If you were to consider alternative approaches, you might also explore: * Using a custom implementation of an array from a Set (e.g., using a `for...in` loop or a `forEach()` callback). * Using a library like Lodash's `fromPairs()` function. * Using a more modern JavaScript feature, such as `Array.from()`, but with additional optimizations or specialized implementations. Keep in mind that the performance difference between these approaches may be small and dependent on specific use cases and environments.
Related benchmarks:
splice vs spread operator for adding elements into very large 2D arrays
Array.from() vs spread []
Spread vs Slice operators in JS
Array.from vs Spread using 1000000 elements / only counts conversion
Array.from vs Spread using 10000 elements / only counts conversion
Comments
Confirm delete:
Do you really want to delete benchmark?