Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs spread operator vs Array.prototype.from
(version: 0)
Compare the new ES6 spread operator with the traditional slice() method
Comparing performance of:
Array.prototype.slice vs spread operator vs Array.prototype.from
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Array.prototype.slice
var params = [ "hello", true, 7 ]; var other = [ 1, 2 ].slice();
spread operator
var params = [ "hello", true, 7 ] var other = [ ...params ]
Array.prototype.from
var params = [ "hello", true, 7 ] var other = Array.from(params.values())
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.slice
spread operator
Array.prototype.from
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 JSON and explain what is being tested, compared options, pros and cons of each approach, and other considerations. **Benchmark Definition** The benchmark defines three test cases to compare the performance of: 1. `Array.prototype.slice()` 2. The new ES6 spread operator (`[ ...params ]`) 3. `Array.prototype.from()` with a `values()` method These test cases aim to measure the execution time and speed of each approach when creating a shallow copy of an array containing different types of values (strings, booleans, numbers). **Test Case 1: Array.prototype.slice()** The benchmark starts by defining a test case for `Array.prototype.slice()`. In this test, an array `params` is created with three elements: "hello", `true`, and `7`. Then, another array `other` is created using the `slice()` method. The test aims to measure how long it takes to create this copy. **Test Case 2: Spread Operator** The second test case uses the new ES6 spread operator (`[ ...params ]`) to create a shallow copy of the `params` array. This test measures the execution time of creating an array with elements extracted from the original `params` array using the spread operator. **Test Case 3: Array.prototype.from()** The third and final test case uses `Array.prototype.from()` with a `values()` method to create a shallow copy of the `params` array. This approach creates an iterator that yields values from the original array, which are then used to construct a new array. The benchmark measures how long it takes to execute this process. **Comparison and Considerations** When choosing between these approaches, consider the following: * **Performance:** The spread operator (`[ ...params ]`) is generally faster than `Array.prototype.slice()`, as it creates a new array with references to the original elements. On the other hand, `Array.prototype.from()` can be slower due to its iterative nature and the need to create an iterator. * **Memory Usage:** All three approaches create a shallow copy of the original array, but the spread operator uses less memory since it only stores references to the original elements. * **Readability and Maintainability:** The spread operator can make code more readable by clearly expressing the intent of creating a new array with extracted values. `Array.prototype.slice()` can be more concise, but its usage might not be immediately clear to developers unfamiliar with this method. **Libraries and Special Features** There is no specific library used in these test cases. However, it's worth noting that `Array.from()` relies on the built-in `values()` method of arrays, which was introduced in ECMAScript 2017 (ES7). No special JavaScript features or syntax are used beyond the standard ES6 spread operator and `Array.prototype.slice()`.
Related benchmarks:
Array.prototype.slice vs spread operator.
Array.prototype.slice vs spread operator with length limit
Array.prototype.slice vs spread operator on a bigger array
Array.prototype.slice vs spread operator - large array 100000
Array.prototype.slice vs spread operator performance
Comments
Confirm delete:
Do you really want to delete benchmark?