8

The simple code below produces RangeError: Maximum call stack size exceeded

const arr = []
for (let i = 0; i < 135000; i++) {
    arr.push(i)
}
const arr2 = []
// something else here that changes arr2
arr2.push(...arr)

1) Why does this happen? (I am simply adding element to an array, why does it increase stack size ?)

2) How to fix this error ? (My goal is to create a shallow copy of arr into arr2)

TSR
  • 12,583
  • 19
  • 64
  • 146
  • You might want to use [`.concat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) instead. – ChrisG May 11 '20 at 22:40
  • 2
    Does this answer your question? [Is there a max number of arguments JavaScript functions can accept?](https://stackoverflow.com/questions/22747068/is-there-a-max-number-of-arguments-javascript-functions-can-accept) – ggorlen May 11 '20 at 22:46

2 Answers2

9

The spread operator there pushes all elements in the original array into the stack, just like .apply:

const arr = [];

for (let i = 0; i < 10; i++) {
  arr.push(i);
}

const arr2 = [];

// Something else here that changes arr2:
arr2.push(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

Array.prototype.push.apply(arr2, arr);

console.log(arr2.join(', '));

So the amount of data you can handle in both cases is limited by the stack size:

const arr = [];

for (let i = 0; i < 135000; i++) {
  arr.push(i);
}

const arr2 = [];

// Something else here that changes arr2:
arr2.push(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

Array.prototype.push.apply(arr2, arr);

console.log(arr.length, arr2.length);

You could do this instead:

const arr = [];

for (let i = 0; i < 135000; i++) {
  arr.push(i);
}

let arr2 = [];

// Something else here that changes arr2:
arr2.push(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

arr2 = [...arr2, ...arr];

console.log(arr.length, arr2.length);
Danziger
  • 16,123
  • 4
  • 41
  • 70
0

//I hope this will help you to make shallow copy of arr into arr2

let arr = []
for (let i = 0; i < 135000; i++) {
    arr.push(i)
}
let arr2 = []
// something else here that changes arr2
arr2=arr

console.log(arr2[0],arr[0]);
//Both 0,0
arr[0]=100

console.log(arr[0],arr2[0])

//Both 100,100
jobayersozib
  • 393
  • 1
  • 7