Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Clone Array - 07/02/2024
(version: 0)
Comparing performance of:
Spread operator clone vs Structured clone
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = [1, 2, 3, , 5, 6, 7, 7, 8, 9, 10]; var arrCopy = null;
Tests:
Spread operator clone
arrCopy = [...arr1];
Structured clone
arrCopy = structuredClone(arr1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread operator clone
Structured clone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread operator clone
6054960.5 Ops/sec
Structured clone
344558.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and provide insights on what's being tested. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark that tests two approaches for cloning an array: the spread operator (`[...arr1]`) and the `structuredClone` function. **Script Preparation Code** The script preparation code defines an array `arr1` with 10 elements, including some null values. This array is used as the input for the benchmarking test cases. **Html Preparation Code** There is no HTML preparation code provided, which means that only JavaScript performance testing will be performed on this benchmark. **Benchmark Test Cases** The benchmark consists of two individual test cases: 1. **Spread Operator Clone**: This test case measures the performance of cloning an array using the spread operator (`[...arr1]`). The idea behind this approach is to create a new array by iterating over each element in `arr1` and copying it to the new array. 2. **Structured Clone**: This test case measures the performance of cloning an array using the `structuredClone` function, which was introduced in ECMAScript 2020 as a way to create a deep copy of an object or array without recursively traversing the data structure. **Libraries and Special JS Features** In this benchmark, no special JavaScript features or syntax are being tested. However, it's worth noting that the `structuredClone` function is a relatively new addition to the JavaScript language, so its performance characteristics may still be evolving. **Pros and Cons of Different Approaches** Here are some pros and cons of each approach: * **Spread Operator Clone**: + Pros: Simple and intuitive, works well for small arrays. + Cons: Can be slow for large arrays due to the need to create a new array and iterate over each element. * **Structured Clone**: + Pros: Fast and efficient for cloning arrays, as it uses optimized internal algorithms. + Cons: May not work correctly with certain types of data or objects. **Other Alternatives** If you're looking for alternative approaches to cloning an array in JavaScript, some options include: * Using `Array.prototype.slice()` or `Array.prototype.concat()`: These methods can be used to create a shallow copy of an array, but may not work correctly if the array contains nested objects or arrays. * Using a library like Lodash's `cloneDeep()` function: This function provides a robust and efficient way to clone complex data structures. **Conclusion** In summary, this benchmark tests two approaches for cloning an array in JavaScript: the spread operator (`[...arr1]`) and the `structuredClone` function. The spread operator approach is simple but may be slow for large arrays, while the structured clone approach is fast and efficient but may not work correctly with certain types of data or objects.
Related benchmarks:
Array clone
Deep cloning of arrays
Deep cloning of arrays with objects
Clone Array - 08/02/2024
Comments
Confirm delete:
Do you really want to delete benchmark?