EN
JavaScript - array.push(...items) causes "Uncaught RangeError: Maximum call stack size exceeded"
1
answers
8
points
Today, working with Preact, I have noticed the exception caused by push()
used with spread syntax (...
).
I have decided to repeat the test on the simpler case, ao the script that I have tested under Google Chrome 97.0.4676:
<script>
const array = Array(500000).fill(0);
const backup = Array();
backup.push(...array);
console.log(array.length);
console.log(backup.length);
</script>
Exception:
push_test.htm:6 Uncaught RangeError: Maximum call stack size exceeded
at push_test.htm:6
Screenshot:

Any ideas why it is not permitted to add items this way? Is there some recurrence inside?
1 answer
7
points
In JavaScript, functions have a limited number of the arguments per call.
In above case array.push(...items)
passes big amount of aruments during call.
It is the same case as you are not able to use array.push.apply(array, items)
function call passing items
array - same problem.
Try to use loops:
<script>
const array = Array(500000).fill(0);
const backup = Array();
for (let i = 0; i < array.length; ++i)
backup.push(array[i]);
console.log(array.length);
console.log(backup.length);
</script>
Preview:

0 comments
Add comment