Try
arr.splice(insertAt,5, ...arrB)
let arr = new Array(25);
console.log(arr);
let arrB = Array(5).fill(1);
let insertAt = 5;
arr.splice(insertAt,5, ...arrB)
console.log(arr);
Following by MDN documentation The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements. syntax: arr.splice(start[, deleteCount[, item1[, item2[, ...]]]])
. Example usage in above snippet
UPDATE
Indeed splice is standard way, but it is slower than for loop - I perform test to check it HERE. Splice is ~28% slower than for-loop.
If your array contains float numbers then you can use Float32Array or Uint32array which is almost 2x faster that Array
(splice is not supported for chrome)
let arr = new Float32Array(25);
console.log(arr);
let arrB = new Float32Array(5).fill(1);
let insertAt = 5;
for(let ix = 0; ix < arrB.length; ix++)
arr[ix + insertAt] = arrB[ix];
console.log(arr);
UPDATE 2
I read you answer and make comparision with Uint32Array
(in case if you wish to use array with integers) - it is 2x faster than normal Array - here.
Uint32Array.prototype.copyInto = function(arr,ix = 0) {
for(let i = 0; i < arr.length; i++)
this[ix+i] = arr[i];
return this;
}
let a = new Uint32Array(2).fill(1);
let x = new Uint32Array(5).fill(0).copyInto(a,2);
console.log(x);