Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.slice vs spread operator new
(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 params = [ "hello", true, 7 ]; var other = params.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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Array.prototype.slice
91.3M/s
spread operator
68.5M/s
View exact numbers
Test name
Executions per second
✓
Array.prototype.slice
91,279,432 Ops/sec
spread operator
68,460,208 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is being tested?** The provided JSON represents a benchmark test case that compares two approaches for creating a copy of an array: the traditional `Array.prototype.slice()` method and the new ES6 spread operator (`[ ...array ]`). **Options compared:** Two options are being compared: 1. **Traditional `Array.prototype.slice()`**: This method returns a shallow copy of a portion of an array, allowing for modifications to be made to the original array without affecting the copied array. 2. **New ES6 spread operator (`[ ...array ]`)**: This method creates a new array by spreading the elements of the original array. **Pros and Cons:** * **Traditional `Array.prototype.slice()`**: + Pros: - Widely supported across different browsers and versions - Can be used to create shallow copies of arrays, which can be useful in certain scenarios + Cons: - May not work as expected when dealing with objects that contain functions or other complex data structures - Can be slower than the spread operator for large datasets * **New ES6 spread operator (`[ ...array ]`)**: + Pros: - Fast and efficient, making it suitable for large datasets - Can handle complex data structures without issues + Cons: - Not widely supported across older browsers and versions (although it's getting more common) - May not be compatible with all JavaScript environments or transpilers **Library:** There is no explicit library mentioned in the provided JSON. However, it's worth noting that some implementations of the spread operator might use libraries like `lodash` for its implementation. **Special JS feature or syntax:** The spread operator (`[ ...array ]`) is a relatively recent addition to JavaScript, introduced in ECMAScript 2015 (ES6). It allows for creating new arrays by spreading the elements of an existing array. **Other alternatives:** If you're looking for alternative approaches to create array copies, consider using: * `Array.prototype.concat()` (although it's generally slower than both `slice()` and the spread operator) * Third-party libraries like `lodash` or `underscore`, which provide additional utility methods for working with arrays In conclusion, the provided benchmark test case compares two popular approaches for creating array copies in JavaScript: the traditional `Array.prototype.slice()` method and the new ES6 spread operator. The choice of approach depends on your specific use case, performance requirements, and compatibility considerations.
Related benchmarks
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?