admin管理员组

文章数量:1287776

In Chrome and Node, the following code throws an error:

function noop() {}
var a = new Array(1e6)
// Array[1000000]
noop.apply(null, a)
// Uncaught RangeError: Maximum call stack size exceeded

I understand why it might be a Bad Idea to pass 1 million arguments to a function, but can anyone explain why the error is Maximum call stack size exceeded instead of something more relevant?

(In case this seems frivolous, the original case was Math.max.apply(Math, lotsOfNumbers), which is a not-unreasonable way of getting the max number from an array.)

In Chrome and Node, the following code throws an error:

function noop() {}
var a = new Array(1e6)
// Array[1000000]
noop.apply(null, a)
// Uncaught RangeError: Maximum call stack size exceeded

I understand why it might be a Bad Idea to pass 1 million arguments to a function, but can anyone explain why the error is Maximum call stack size exceeded instead of something more relevant?

(In case this seems frivolous, the original case was Math.max.apply(Math, lotsOfNumbers), which is a not-unreasonable way of getting the max number from an array.)

Share Improve this question asked Feb 16, 2017 at 1:16 nrabinowitznrabinowitz 55.7k10 gold badges151 silver badges169 bronze badges 2
  • …because function arguments are stored on the stack? – Bergi Commented Feb 16, 2017 at 2:18
  • Well, yes, that's the piece of info I was missing :) – nrabinowitz Commented Feb 16, 2017 at 3:44
Add a ment  | 

1 Answer 1

Reset to default 13

Function arguments are put on the stack. You're trying to put a million arguments onto the stack, and that's more than the maximum stack size. So the error message is very relevant to the reason for the error.

本文标签: