Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.from vs Spread vs Slice
(version: 0)
Comparing performance of:
Array.from vs Spread vs .slice
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
Array.from
var fooSet = []; for(var i=0;i<100;i++) { fooSet.push(i); } var other = Array.from(fooSet);
Spread
var fooSet = []; for(var i=0;i<100;i++) { fooSet.push(i); } var other = [...fooSet];
.slice
var fooSet = []; for(var i=0;i<100;i++) { fooSet.push(i); } var other = fooSet.slice();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.from
Spread
.slice
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 provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark is designed to compare the performance of three different ways to create an array from a set of numbers: `Array.from()`, spread operator (`...`), and `slice()` methods. **Comparison** 1. **`Array.from()`**: Creates a new array by copying all elements from the provided iterable (in this case, a closure that returns an array with 100 elements). 2. **Spread Operator (`...`)**: Creates a new array by spreading all elements from the provided iterable (in this case, an array with 100 elements) into a new array. 3. **`.slice()` method**: Returns a shallow copy of a portion of an array (in this case, a new array containing all elements from the original array). **Pros and Cons** * **`Array.from()`**: Pros: * Creates a new array without modifying the original iterable. * Allows for custom mapping or filtering functions. * Cons: * May incur additional overhead due to function call and argument creation. * **Spread Operator (`...`)**: Pros: * Very concise and readable syntax. * Can be used as a shorthand for creating arrays from iterables. * Cons: * Creates a new array, which may not be desirable in certain situations (e.g., when working with large datasets). * Can lead to unexpected behavior if not used carefully (e.g., when dealing with nested arrays or objects). * **`.slice()` method**: Pros: * Fast and efficient for creating shallow copies of arrays. * Can be used to extract a portion of an array without modifying the original. Cons: * May incur additional overhead due to indexing calculations. * Returns a shallow copy, which may not preserve the original data structure (e.g., if the underlying data is an object with nested properties). **Library and Special JS Features** In this benchmark, there are no libraries used beyond the built-in JavaScript features. However, the use of `closure` in the benchmark definition is noteworthy. A closure is a function that has access to its own scope (including variables) and the scope of its outer functions, even when called outside those scopes. The code uses a closure to create an array with 100 elements, which is then passed to `Array.from()`, spread operator, or `.slice()` method for comparison. **Other Alternatives** If you need to create arrays from iterables in JavaScript, other alternatives include: * **`map()` and `reduce()` methods**: While not directly related to array creation, these methods can be used to transform and accumulate data into an array. * **`Array.prototype.push()` and `concat()` methods**: Can be used to add elements to an array or concatenate multiple arrays into one. * **Native support for array literals**: In modern JavaScript, you can use the square bracket notation (`[]`) to create arrays directly. Each of these alternatives has its own strengths and weaknesses, depending on the specific requirements of your project.
Related benchmarks:
Array.prototype.slice vs spread operator with length limit
Array clone from index 1 to end: spread operator vs slice
Array.prototype.slice vs spread operator on a bigger array
Array.prototype.slice vs spread op
Array.prototype.slice vs spread operator - large array 100000
Comments
Confirm delete:
Do you really want to delete benchmark?