2

In javascript, lets say I have a preallocated array of n items, and I have another array that I want to copy into the first array at a given starting index, the following is one way to do it:

let arr = new Array(25);
console.log(arr);

let arrB = Array(5).fill(1);
let insertAt = 5;
for(let ix = 0; ix < arrB.length; ix++)
  arr[ix + insertAt] = arrB[ix];
console.log(arr);

Is there a more efficient / standard way of doing this?

I am thinking of something equivalent to the following in C++: http://www.cplusplus.com/forum/general/199358/

6

4 Answers 4

2

Efficiency wise, I do not think there is a better way than the code you posted. You will need to go through all the items in the array that need to be copied.

I agree with others in that using slice is probably the standard way of doing this.

1

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);

0
1

Not sure how performance efficient this code will be (As am still a learner) but proposing it as another way of achieving such result.

let arr = new Array(25);
let arrB = Array(5).fill(1);
let insertAt = 5;

function copy(index) {
    if (arrB.length === 0 || index > 9) {
        return;
    }

    arr[index] = arrB.shift();
    copy(index + 1);
}

copy(insertAt);
0

I ended up making a module to make this easier:

const checks = require("checks"); //NB, NOT ON NPM....

(()=>{
  Array.prototype.copyInto = function(arr,ix = 0){
    if(!checks.isArray(arr))
      throw new Error("'arr' argument must be an array");
    if(!checks.isInteger(ix) || ix < 0)
      throw new Error("'ix' must be a positive integer");
    for(let i = 0; i < arr.length; i++)
      this[ix+i] = arr[i];
    return this;
  }
})();

which means it can be used like this:

let x = Array(5).fill(0).copyInto([1,1],2);
console.log(x);

Not sure if this is the right approach, but it works for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.