Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Shallow Copy
(version: 0)
Comparing performance of:
Object.values vs slice vs loop vs Concat vs Array From
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; for(let i=0;i<1000000;i++){ arr[i]=Math.random(); }
Tests:
Object.values
let arr2 = Object.values(arr)
slice
let arr2 = arr.slice(0)
loop
let arrLength = arr.length; let arr2=new Array(arrLength); for(let i=0;i<arrLength;i++){ arr2[i]=arr[i] }
Concat
let arr2 = arr.concat([])
Array From
let arr2 = Array.from(arr)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Object.values
slice
loop
Concat
Array From
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):
Let's break down the benchmark and its test cases. **What is being tested?** The benchmark measures how fast different ways of creating a shallow copy of an array are. A shallow copy of an array in JavaScript means creating a new array with references to the same elements as the original array, without copying the values themselves. **Options compared:** 1. `Object.values(arr)`: This method returns an array containing all the property values of the given object (in this case, an array). 2. `arr.slice(0)` : The `slice()` method creates a new array with references to the same elements as the original array. 3. `loop`: This approach manually iterates over the array and assigns each element to a new array. 4. `arr.concat([])` : The `concat()` method creates a new array by concatenating the given array with another array (in this case, an empty array). 5. `Array.from(arr)`: This method creates a new array from an iterable or an array-like object. **Pros and Cons of each approach:** 1. **Object.values**: Fast and concise, but it returns an array containing all property values, not a shallow copy of the original array. 2. **slice(0)** : Creates a shallow copy of the original array, efficient in terms of memory usage, and easy to read. 3. **loop**: Manual iteration can be error-prone and less readable than other approaches. 4. **concat([])** : Creates a new array with references to the same elements as the original array, but it's less efficient than `slice(0)` since it creates an extra copy of the array. 5. **Array.from(arr)**: Similar to `slice(0)`, but uses a more modern and concise syntax. **Library usage:** None of the test cases use external libraries. **Special JS feature or syntax:** * `Object.values` is a relatively new method introduced in ECMAScript 2017 (ES7). It's not widely supported in older browsers. * `Array.from(arr)` is also a modern method introduced in ECMAScript 2015 (ES6). **Other alternatives:** In addition to the above methods, there are other ways to create a shallow copy of an array, such as: * Using `Array.prototype.map()` and then spreading the resulting array using the spread operator (`...`): `arr.map(x => x)` followed by `[...arr.map(x => x)]`. * Using `lodash's_.cloneDeep()` (not used in this benchmark). * Manually iterating over the array like the "loop" approach. It's worth noting that for large arrays, some methods may have performance implications due to memory allocation or copy overhead. However, for small to medium-sized arrays, the differences between these methods are usually negligible.
Related benchmarks:
Fill array with random integers
Preinitialized array size vs Push operations to an empty one.
Array copy
Array cloning 2023
Comments
Confirm delete:
Do you really want to delete benchmark?