New issue
Advanced search Search tips

Issue 810740 link

Starred by 3 users

Issue metadata

Status: WontFix
Owner: ----
Closed: Feb 2018
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: Windows
Pri: 2
Type: Bug



Sign in to add a comment

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
 
Components: -Blink Blink>JavaScript
Labels: Needs-Triage-M64
Labels: Triaged-ET Needs-Feedback
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!
810740.PNG
108 KB View Download

Comment 4 by woxxom@gmail.com, 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;
  }

Comment 5 by timo...@gmail.com, 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);
Project Member

Comment 6 by sheriffbot@chromium.org, Feb 15 2018

Cc: viswa.karala@chromium.org
Labels: -Needs-Feedback
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

Comment 7 by timo...@gmail.com, 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.

Comment 8 by woxxom@gmail.com, 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.
Cc: bmeu...@chromium.org
Status: WontFix (was: Unconfirmed)
Thanks for the report. Please see #8 for an assessement. 

Sign in to add a comment