Maximum call stack size exceeded when Array.prototype.unshift.apply
Reported by
timo...@gmail.com,
Feb 9 2018
|
|||||
Issue description
UserAgent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0
Steps to reproduce the problem:
Just execute the following code:
```
var array = [];
for(var i=0; i < 150000; i++) {
array.push(1);
}
Array.prototype.unshift.apply([], t);
```
What is the expected behavior?
No errors.
What went wrong?
Uncaught RangeError: Maximum call stack size exceeded
Did this work before? N/A
Chrome version: 64.0.3282.140 Channel: stable
OS Version: 10.0
Flash Version: 28.0.0.161
,
Feb 11 2018
,
Feb 13 2018
Tested the issue on chrome reported version 64.0.3282.140 using Ubuntu 14.04 with steps mentioned below.
1) Launched chrome reported version opened Devtools > Console
2) When we executed the code:
var array = [];
for(var i=0; i < 150000; i++) {
array.push(1);
}
Array.prototype.unshift.apply([], t);
Output: Uncaught ReferenceError: t is not defined at <anonymous>:5:35
3) When we executed the code:
var array = [];
for(var i=0; i < 150000; i++) {
array.push(1);
}
Output: 150000
@Reporter:
Please find the attached screen shot for your reference and let us know if we missed anything in reproducing the issue, provide your inputs on it for further triaging it in better way
Thanks!
,
Feb 13 2018
Most browsers have the stack limit less than 100k so this is working as intended.
The proper solution (for cross-browser JavaScript) has always been to use Array#concat in such cases, for example:
var result;
if (array.length > 1000) {
result = b.concat(a);
} else {
Array.prototype.unshift.apply(a, b);
result = a;
}
,
Feb 15 2018
Oh, sorry for incorrect steps to reproduce! It is a mistake. Here is valid code:
var array = [];
for(var i=0; i < 150000; i++) {
array.push(1);
}
Array.prototype.unshift.apply([], array);
,
Feb 15 2018
Thank you for providing more feedback. Adding requester "viswa.karala@chromium.org" to the cc list and removing "Needs-Feedback" label. For more details visit https://www.chromium.org/issue-tracking/autotriage - Your friendly Sheriffbot
,
Feb 15 2018
> The proper solution (for cross-browser JavaScript) has always been to use Array#concat in such cases Sure, but actually in some cases we cannot change the ref to an array (it is not my case, but I believe there is somebody who needs to use such way to modify array). > Most browsers have the stack limit less than 100k so this is working as intended. Sure, but why this code uses stack for work? In Firefox it works fine without RangeError.
,
Feb 15 2018
1) You can make a temporary array of the source if it cannot be changed, that's trivial. 2a) Function#apply simply passes the array as the function arguments so the stack is used because that's how arguments are passed. 2b) Different browsers have different limits of the stack. It even varies by the version.
,
Feb 20 2018
Thanks for the report. Please see #8 for an assessement. |
|||||
►
Sign in to add a comment |
|||||
Comment 1 by dtapu...@chromium.org
, Feb 9 2018