Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

[].push.apply([], a) results in "Out of stack space" #521

Closed
Tracked by #3087
alexlamsl opened this issue Apr 24, 2018 · 3 comments
Closed
Tracked by #3087

[].push.apply([], a) results in "Out of stack space" #521

alexlamsl opened this issue Apr 24, 2018 · 3 comments

Comments

@alexlamsl
Copy link

  • Version: 8.11.1
  • Platform: Windows 10 x64

test.js

var a = [];
for (var i = 65536; --i >= 0;) a.push(i);
[].push.apply([], a);

node runs to completion, whereas node-chakracore fails with:

$ node test.js
test.js:3
[].push.apply([], a);
^

Error: Out of stack space
   at Anonymous function (test.js:3:1)
   at Module.prototype._compile (module.js:652:5)
   at Module._extensions[.js] (module.js:663:3)
   at Module.prototype.load (module.js:565:3)
   at tryModuleLoad (module.js:505:5)
   at Module._load (module.js:497:3)
   at Module.runMain (module.js:693:3)
   at startup (bootstrap_node.js:189:9)
   at Anonymous function (bootstrap_node.js:613:3)

I have observed failures with shorter lengths than 65536, but they are not as reliably reproducible. While I appreciate this is a lot of parameters to pass on to Function.apply(), it is an efficient way to construct a large array, e.g. https://github.com/mishoo/UglifyJS2/blob/b5bab254ce2122a43e9ca0fdc757aecda7191576/test/mocha/parentheses.js#L93-L95

@MSLaguana
Copy link
Contributor

I'm curious why you use push rather than concat:

var start = process.hrtime();
var c = [1,2];
for(let i = 0; i < 16; ++i) {c = [].concat.call(c,c)}
console.log(process.hrtime(start))

And actually, comparing with node-v8 on the sample in your mocha test, the concat version is faster in addition to using less stack.

@alexlamsl
Copy link
Author

I did push() instead of concat() to minimise memory allocation, though I haven't considered call() being faster than apply().

Thanks for the workaround – will apply to mishoo/UglifyJS#3087

@alexlamsl
Copy link
Author

I shall close this since it's a special case and more to do with deficiencies of ChakraCore than this project.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants