Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Copy Array
(version: 0)
Speed of array cloning
Comparing performance of:
Spread vs Slice vs Slice(0) vs concat
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var len = 1000*1000; var arr = new Array(len);
Tests:
Spread
var arr2 = [...arr];
Slice
var arr2 = arr.slice();
Slice(0)
var arr2 = arr.slice(0);
concat
var arr2 = arr.concat();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Spread
Slice
Slice(0)
concat
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread
1459.1 Ops/sec
Slice
5953.3 Ops/sec
Slice(0)
5894.7 Ops/sec
concat
5853.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided JSON represents a benchmark named "Copy Array". The benchmark measures the speed of array cloning, which is essential in various scenarios such as data processing, algorithm optimization, and performance-critical code. **Script Preparation Code** The script preparation code initializes an array with a length of 1 million elements: `var len = 1000*1000;\r\nvar arr = new Array(len);`. This ensures that the benchmark is run with a large, consistent array size. **Html Preparation Code** There is no HTML preparation code provided in this benchmark. The focus is solely on the JavaScript code. **Test Cases** The benchmark consists of four test cases: 1. **Spread**: `var arr2 = [...arr];` 2. **Slice**: `var arr2 = arr.slice();` 3. **Slice(0)**: `var arr2 = arr.slice(0);` 4. **Concat**: `var arr2 = arr.concat();` These test cases compare the performance of different methods for cloning an array. **Options Compared** Each test case compares the performance of a specific method: * Spread (`[...arr]`) * Slice (`arr.slice()`) * Slice with a starting index (`arr.slice(0)`) * Concat (`arr.concat()`) **Pros and Cons** Here's a brief overview of each approach: * **Spread**: Creates a new array by spreading the elements of `arr` into an array. Pros: concise, modern syntax. Cons: can be slower due to the overhead of creating a new array. * **Slice**: Returns a shallow copy of `arr` from the specified index to the end of the array. Pros: efficient for large arrays, as it only copies the needed elements. Cons: returns a reference to the original array, which might not be desirable in some cases. * **Slice(0)**: Similar to Slice, but starts from the beginning of the array. Pros: identical performance to Slice for arrays with no gaps. Cons: unnecessary for this use case. * **Concat**: Returns a new array that is the concatenation of `arr` and an empty array. Pros: creates a new, self-contained array. Cons: slower than Slice due to the overhead of creating a new array. **Library Usage** There are no libraries explicitly mentioned in the benchmark definition or test cases. **Special JS Features/Syntax** None of the test cases use any special JavaScript features or syntax beyond what's considered standard for modern browsers (ECMAScript 2015+). **Other Alternatives** If you're interested in optimizing array cloning, consider using: * `Array.from(arr)`: A modern method that creates a new array from an iterable. * `Array.prototype.map()`: Can be used to create a new array by mapping over the original array. * Native WebAssembly (WASM) or other low-level optimizations for performance-critical code. Keep in mind that these alternatives might have trade-offs in terms of readability, maintainability, and compatibility with older browsers.
Related benchmarks:
Shallow Copy Array
Deep cloning of arrays
shallow copy of 6M elements array
Array cloning 2023
Comments
Confirm delete:
Do you really want to delete benchmark?