Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs spread operator (better though)
(version: 0)
Compare the new ES6 spread operator with the traditional slice() method
Comparing performance of:
Array.prototype.slice vs spread operator
Created:
2 years ago
by:
Guest
Go to the latest result
Tests:
Array.prototype.slice
var other = [ "hello", true, 7 ].slice();
spread operator
var params = [ "hello", true, 7 ] var other = [ ...params ]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.slice
spread operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 124 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Array.prototype.slice
27.5M/s
spread operator
18.7M/s
View exact numbers
Test name
Executions per second
✓
Array.prototype.slice
27,539,580 Ops/sec
spread operator
18,695,802 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! The provided JSON represents a benchmarking test case that compares two approaches for creating a shallow copy of an array in JavaScript: `Array.prototype.slice()` and the new ES6 spread operator (`...`). **What is being tested?** In this test, we're measuring which approach is faster and more efficient. The specific benchmark definition files are: 1. `var other = [ "hello", true, 7 ].slice();` - This code uses the traditional `Array.prototype.slice()` method to create a copy of the array. 2. `var params = [ "hello", true, 7 ]; var other = [ ...params ];` - This code uses the spread operator (`...`) to create a new array with the same elements as the original `params` array. **Options compared** We have two options being compared: * **`Array.prototype.slice()`**: A traditional method for creating a shallow copy of an array. * **Spread operator (`...`)**: A newer, more concise way of creating arrays in JavaScript (introduced in ECMAScript 2015). **Pros and Cons** Here are some pros and cons of each approach: **`Array.prototype.slice()`** Pros: * Well-established and widely supported method. * Easy to understand and use for most developers. Cons: * Can be slower than the spread operator, especially for large arrays. **Spread operator (`...`)** Pros: * More concise and expressive syntax. * Can be faster for large arrays due to its optimized implementation. Cons: * Introduced in ECMAScript 2015, which means older browsers may not support it. * May require additional imports or polyfills for older browsers. **Other considerations** * **Library usage**: Neither of these approaches uses any external libraries. They are built-in JavaScript features. * **Special JS feature/syntax**: The spread operator (`...`) is a new syntax introduced in ECMAScript 2015, which allows it to be used with arrays and other iterables. **Alternative approaches** Other alternatives for creating shallow copies of arrays include: * `Array.prototype.concat()` - This method concatenates the elements of an array, but can be slower than both `slice()` and the spread operator. * `Object.assign()` - This method creates a new object by copying elements from another object. While it can work for arrays, it's not as efficient as `slice()` or the spread operator. In summary, the benchmark is testing which approach (traditional `Array.prototype.slice()`, or the new ES6 spread operator) is faster and more efficient when creating shallow copies of arrays in JavaScript.
Related benchmarks
Array.prototype.slice vs spread operator.
Array.prototype.slice vs spread operator With slightly bigger array
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?