I just ran a benchmark of 4 algorithms, on Chrome:
in-place:
// 1) splice method
{
let x = [8, 4, 1, 4, 124, 1, 14, 11, 9, 100, 6, 44];
const y = [5, 6, 99, 5, 3, 4];
x.splice(0, 0, ...y); // 87'426 ops/s (but big variation of 35%)
// x is [5, 6, 99, 5, 3, 4, 8, 4, 1, 4, 124, 1, 14, 11, 9, 100, 6, 44]
}
// 2) unshift method
{
let x = [8, 4, 1, 4, 124, 1, 14, 11, 9, 100, 6, 44];
const y = [5, 6, 99, 5, 3, 4];
x.unshift(...y); // 69'471 ops/s
// x is [5, 6, 99, 5, 3, 4, 8, 4, 1, 4, 124, 1, 14, 11, 9, 100, 6, 44]
}
copy:
// 3) spread operator
{
const x = [8, 4, 1, 4, 124, 1, 14, 11, 9, 100, 6, 44];
const y = [5, 6, 99, 5, 3, 4];
const z = [...y, ...x]; // 17'118 ops/s
// z is [5, 6, 99, 5, 3, 4, 8, 4, 1, 4, 124, 1, 14, 11, 9, 100, 6, 44]
}
// 4) concat method
{
const x = [8, 4, 1, 4, 124, 1, 14, 11, 9, 100, 6, 44];
const y = [5, 6, 99, 5, 3, 4];
const z = y.concat(x); // 6'286 ops/s
// z is [5, 6, 99, 5, 3, 4, 8, 4, 1, 4, 124, 1, 14, 11, 9, 100, 6, 44]
}
Summary: if you want to preprend in place, both unshift and splice are good, and if you want a copy, then the spread operator seems the best choice ... on Chrome at least.