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.
const getInitialArray = () => {return [[1, 2], [3, 4]}
– Salyer