Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop

javascript clone array of objects
javascript clone object
javascript clone array without reference
javascript duplicate array elements
best way to deep clone array in javascript
lodash clone array
typescript copy array
write a javascript program to clone an array

In order to duplicate an array in JavaScript: which of the following is faster to use?

Slice method
var dup_array = original_array.slice();
For loop
for(var i = 0, len = original_array.length; i < len; ++i)
   dup_array[i] = original_array[i];

I know both ways do only a shallow copy: if original_array contains references to objects, objects won't be cloned, but only the references will be copied, and therefore both arrays will have references to the same objects. But this is not the point of this question.

I'm asking only about speed.

Speed Alley: The fastest way to copy an array, In order to duplicate an array in JavaScript: which of the following is faster to use? Slice method. var dup_array = original_array.slice();. For  It's not about speed, but comfort. Besides as you can see you can only use slice(0) on primitive types. To make an independent copy of an array rather than a copy of the refence to it, you can use the array slice method. Example: To make an independent copy of an array rather than a copy of the refence to it, you can use the array slice method.

Technically slice is the fastest way. However, it is even faster if you add the 0 begin index.

myArray.slice(0);

is faster than

myArray.slice();

http://jsperf.com/cloning-arrays/3

Fastest way to duplicate an array in JavaScript, In order to duplicate an array in JavaScript: which of the following is faster to use? Slice method. var dup_array = original_array.slice();. For loop. for(var i = 0, len  Slice is a copy method! In other words it copies the values/references from one array to another. So when you copy objects from array A to B and modify a single item that is referenced in both arrays. Both array A and B will display the new values for this item. Cloning has a different meaning.

what about es6 way?

arr2 = [...arr1];

How to clone an array in JavaScript, I've written on 10 Ways to Write pipe/compose in JavaScript, and now we're slice returns a shallow copy of an array based on the provided  Stack Overflow Public questions and answers; Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop Find object by id in an array of

Easiest way to deep clone Array or Object:

var dup_array = JSON.parse(JSON.stringify(original_array))

Cloning JavaScript Array in Multiple Ways, Slice. For a long time this has been the most used and the fastest way to clone an array. Slice should still be your preferred way to clone  I need to make a copy of a slice in Go and reading the docs there is a copy function at my disposal. The copy built-in function copies elements from a source slice into a destination slice. (As a special case, it also will copy bytes from a string to a slice of bytes.) The source and destination may overlap.

var cloned_array = [].concat(target_array);

Fastest way to duplicate an array in JavaScript - slice vs , In order to duplicate an array in JavaScript: which of the following is faster to use? Slice method. var dup_array = original_array.slice();. For loop. for(var i = 0, len  The simplest way to duplicate an array in 2018 is to use the ES6 spread operator. Previously using slice()was probably the best approach. You can still use concatthough the approach isn’t as clean in my opinion, and Array.fromis a fun way to do it, too, just for kicks.

Clone Arrays in JavaScript, _.clone. The spread operator and the slice method are the fastest ways to copy a shallow array, but keep in mind that this does  Because arrays in JS are reference values, so when you try to copy it using the = it will only copy the reference to the original array and not the value of the array. To create a real copy of an array, you need to copy over the value of the array under a new value variable.

ES6 Way to Clone an Array, When we need to copy an array, we often time used `slice`. Problem with Reference Values; Mutable vs Immutable Data Types; Shallow Copy Only Because arrays in JS are reference values, so when you try to copy it using the = it will  How to Deep Copy Objects and Arrays in JavaScript The usual methods of copying an object or array only make a shallow copy, so deeply-nested references are a problem. You need a deep copy if a

Javascript fastest way to duplicate an Array, In order to duplicate an array in JavaScript: which of the following is faster to use? Slice method var dup_array = original_array.slice(); For loop for(var i = 0, len  JSPerf shows that concat method to copy an array is faster than slice #243. JSPerf shows that concat method to copy an array is faster than slice. #243.

Comments
  • jsben.ch/#/wQ9RU <= a benchmark for the most common ways to clone an array
  • @cept0 no emotions, just benchmarks jsperf.com/new-array-vs-splice-vs-slice/31
  • @Dan So what? Your test case results: Firefox 30 nightly is still ~230% faster than Chrome. Check the source code of V8 for splice and you'll be surprised (while...)
  • Sadly for short arrays the answer is vastly different. For example cloning an array of listeners before calling each of them. Those arrays are often small, usually 1 element.
  • You missed this method: A.map(function(e){return e;});
  • You're writing about blink browsers. Isn't blink just a layout engine, mainly affecting HTML rendering, and thus unimportant? I thought we'd rather talk about V8, Spidermonkey and friends here. Just a thing that confused me. Enlighten me, if I'm wrong.
  • And is myArray.slice(0,myArray.length-1); faster than myArray.slice(0); ?
  • @jave.web you;ve just dropped last element of the array. Full copy is array.slice(0) or array.slice(0, array.length)
  • if converted with babel: [].concat(_slice.call(arguments))
  • Not sure where arguments is coming from... I think your babel output is conflating a few different features. It's more likely to be arr2 = [].concat(arr1).
  • @SterlingArcher arr2 = [].conact(arr1) is different from arr2 = [...arr1]. [...arr1] syntax will convert hole to undefined. For example, arr1 = Array(1); arr2 = [...arr1]; arr3 = [].concat(arr1); 0 in arr2 !== 0 in arr3.
  • I tested this in my browser (Chrome 59.0.3071.115) against Dan's answer above. It was more than 10 times slower than .slice(). n = 1000*1000; start = + new Date(); a = Array(n); b = [...a]; console.log(new Date() - start); // 168
  • Still will not clone something like this: [{a: 'a', b: {c: 'c'}}]. If c's value is changed in the "duplicated" array, it will change in the original array, since it's just a referential copy, not a clone.