Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Native slice vs copy
(version: 0)
Array#slice vs for loop with array preallocation
Comparing performance of:
Native vs For Loop
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function copy (a) { const l = a.length const b = new Array(l) for (let i = 0; i < l; ++i) { b[i] = a[i] } return b } function copyNative (a) { return a.slice(0) }
Tests:
Native
var arr = Array(100).fill('hello') var res = copyNative(arr)
For Loop
var arr = Array(100).fill('hello') var res = copy(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native
For Loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Native
5867701.0 Ops/sec
For Loop
3571926.5 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares the performance of two methods for creating a copy of an array in JavaScript: **Method 1: `Array#slice()` (Native)** * This method uses the built-in `slice()` function on arrays to create a shallow copy of the original array. * It's concise and generally considered efficient. ```javascript function copyNative(a) { return a.slice(0); } ``` **Method 2: For Loop with Preallocation (For Loop)** * This method iterates through the original array using a `for` loop and creates a new array of the same length beforehand. * Each element is then copied individually from the source array to the new array. * While this method allows for potential control over memory allocation, it can be less efficient than `slice()` in many cases. ```javascript function copy(a) { const l = a.length; const b = new Array(l); for (let i = 0; i < l; ++i) { b[i] = a[i]; } return b; } ``` **Pros and Cons:** * **`Array#slice()`**: * **Pros**: Concise, efficient in most cases. * **Cons**: Doesn't offer granular control over memory allocation. * **For Loop with Preallocation**: * **Pros**: Potentially more control over memory management (though often not necessary). * **Cons**: Can be less efficient than `slice()`, as it involves more explicit loop operations. **Other Considerations:** * The benchmark tests the performance of these methods for creating a copy of an array containing 100 elements, all filled with the string "hello". * **Alternatives:** There are other techniques for copying arrays, such as using spread syntax (`...arr`) or the `concat()` method. Each approach has its own performance characteristics and use cases. Let me know if you have any more questions about this benchmark or JavaScript array operations in general!
Related benchmarks:
Native slice vs copy
Native slice vs copy
Array shallow copy - slice(0) vs conditional for() loop
Slice vs spread array
Comments
Confirm delete:
Do you really want to delete benchmark?