Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
shallow copy of array
(version: 0)
Comparing performance of:
slice vs push-apply vs the ...operator vs for-push vs concat
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
arr0 = []; for(var i=0;i<10000;i++){ arr0.push("var "+i); }
Tests:
slice
var other = arr0.slice()
push-apply
var other = []; Array.prototype.push.apply(other, arr0);
the ...operator
var other = [...arr0];
for-push
var other = []; for(var i=0;i<arr0.length;i++){ other.push(arr0[i]); }
concat
var other = []; other = other.concat(arr0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
slice
push-apply
the ...operator
for-push
concat
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):
**What is being tested?** MeasureThat.net is testing the performance of different ways to create a shallow copy of an array in JavaScript. The test cases are designed to measure the execution speed of various methods, including: 1. `slice()` 2. `push()` with `apply()` 3. The spread operator (`...`) 4. `for` loop and `push()` **Options compared** The options being compared are different approaches to creating a shallow copy of an array in JavaScript. Each test case has a unique approach, which is measured for its execution speed. **Pros and Cons of each approach:** 1. **`slice()`**: This method creates a new array by copying elements from the original array. Pros: concise and efficient. Cons: may not work as expected if the original array is modified. 2. **`push()` with `apply()`:** This method pushes all elements from the original array onto a new array using the `apply()` method. Pros: can be used with arrays of any type, including nested arrays. Cons: less concise and may have performance overhead due to the use of `apply()`. 3. **The spread operator (`...`):** This method creates a new array by spreading elements from the original array. Pros: concise and efficient. Cons: requires JavaScript 2015 or later for support. 4. **`for` loop and `push()`**: This method pushes all elements from the original array onto a new array using a `for` loop. Pros: can be used with arrays of any type, including nested arrays. Cons: less concise and may have performance overhead due to the use of a `for` loop. **Library usage** None of the test cases explicitly uses a JavaScript library. **Special JS features or syntax** The spread operator (`...`) is used in one of the test cases, which requires JavaScript 2015 or later for support. The other test cases do not use any special features or syntax. **Other alternatives** There are additional ways to create a shallow copy of an array in JavaScript, including: 1. Using `Array.from()`: This method creates a new array by copying elements from the original array. 2. Using a `forEach()` loop: This method pushes all elements from the original array onto a new array using a `forEach()` loop. These alternatives are not included in the test cases provided. I hope this explanation helps! Let me know if you have any further questions.
Related benchmarks:
Copy Array
Shallow Copy Array
shallow copy of 6M elements array
Clone Array - 08/02/2024
Comments
Confirm delete:
Do you really want to delete benchmark?