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
  • array slice? w3schools.com/jsref/jsref_slice_array.asp Commented Jan 21, 2019 at 23:54
  • 1
    splice should fit your use-case developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jan 21, 2019 at 23:54
  • stackoverflow.com/questions/30188970/… Commented Jan 21, 2019 at 23:56
  • There are options that are more succinct, but I think you will be hard pressed to come up with something more efficient than this. Commented Jan 22, 2019 at 0:00
  • Thanks Mark. The above example is just to illustrate my point, in production I have an array of length probably up to 1 million in length , and I might need to insert say 100k entries, so efficiency is my main priority. Commented Jan 22, 2019 at 0:02

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.