Javascript efficient copy array into another array at index - Stack Overflow

In javascript, lets say I have a preallocated array of n items, and I have another array that I want to

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++: /

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./forum/general/199358/

Share Improve this question asked Jan 21, 2019 at 23:49 Nicholas HamiltonNicholas Hamilton 10.6k7 gold badges62 silver badges90 bronze badges 6
  • array slice? w3schools./jsref/jsref_slice_array.asp – danielarend Commented Jan 21, 2019 at 23:54
  • 1 splice should fit your use-case developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – philip yoo Commented Jan 21, 2019 at 23:54
  • stackoverflow./questions/30188970/… – Chris Hawkes Commented Jan 21, 2019 at 23:56
  • There are options that are more succinct, but I think you will be hard pressed to e up with something more efficient than this. – Mark 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. – Nicholas Hamilton Commented Jan 22, 2019 at 0:02
 |  Show 1 more ment

4 Answers 4

Reset to default 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.

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

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744954812a4603129.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信