Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Object.assign vs Spread 2
(version: 0)
Comparing performance of:
Array.from vs Object.assign vs Spread
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
Array.from
const classList = ['a', 'b', 'c'] console.log(Array.from(classList))
Object.assign
const classList = ['a', 'b', 'c'] console.log(Object.assign([], classList))
Spread
const classList = ['a', 'b', 'c'] console.log([...classList])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.from
Object.assign
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 its test cases. The benchmark is testing three different approaches to create an array from an existing iterable: `Array.from()`, `Object.assign()` with an empty array as the first argument, and the spread operator (`...`). **Options being compared:** 1. **Array.from()**: This method takes an iterable (like a string or an array) as its first argument and returns a new array containing all elements of the iterable. 2. **Object.assign() with an empty array**: This approach uses `Object.assign()` to create a shallow copy of the input iterable by concatenating it with an empty array. 3. **Spread operator (`...`)**: The spread operator is used to expand an iterable (like an array or string) into individual elements. **Pros and Cons of each approach:** 1. **Array.from()**: * Pros: Efficient, creates a new array, and can handle nested iterables. * Cons: May be slower than the spread operator for small arrays. 2. **Object.assign() with an empty array**: * Pros: Can handle large datasets and avoids creating a new array. * Cons: Slower than `Array.from()` and may create unnecessary intermediate objects (if not using `Object.assign()` correctly). 3. **Spread operator (`...`)**: * Pros: Fast, efficient, and creates a new array without the overhead of `Object.assign()`. * Cons: May be slower for very large datasets due to the creation of temporary arrays. **Library used in test cases:** None are explicitly mentioned, but the use of `Object.assign()` suggests that it is being used as part of the JavaScript standard library. **Special JS feature or syntax used in test cases:** The spread operator (`...`) is a relatively new feature introduced in ECMAScript 2015 (ES6). It allows you to expand an iterable into individual elements. **Other alternatives:** 1. **Array.prototype.slice()**: This method returns a shallow copy of the input array. 2. **Array.prototype.concat()**: This method concatenates two or more arrays and returns a new array. 3. **For loops**: You could use a traditional for loop to iterate over an iterable and create a new array. In terms of alternatives, `Object.assign()` with an empty array is not as efficient or practical as the spread operator or `Array.from()`. The spread operator is generally considered the most efficient and modern way to create an array from an iterable.
Related benchmarks:
JavaScript spread operator vs Object.assign performance 2 - kevin
JavaScript spread operator vs Object.assign performance with {} target obj
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
JavaScript spread operator vs Object.assign performance v3
Comments
Confirm delete:
Do you really want to delete benchmark?