The fastest way to duplicate an array in JavaScript

The fastest way to duplicate an array in JavaScript depends on the size of the array and the type of elements it contains.

Here are three methods for duplicating an array in JavaScript:

Using the slice method:

const originalArray = [1, 2, 3];
const duplicateArray = originalArray.slice();

Using the spread operator:

const originalArray = [1, 2, 3];
const duplicateArray = [...originalArray];

Using Array.from:

const originalArray = [1, 2, 3];
const duplicateArray = Array.from(originalArray);

All three methods are speedy for small arrays with simple data types like numbers and strings. The slice method and the spread operator are often considered the quickest and most readable methods. The Array.from method is slightly slower but is useful when duplicating arrays with more complex data types like objects and other arrays.

For large arrays, performance may vary based on the type of data contained in the array. In general, the slice the method is considered fast for arrays with simple data types, while the spread operator and Array.from are often faster for arrays with more complex data types like objects and other arrays.