为什么带有太多参数的应用会抛出“超出最大调用堆栈大小"? [英] Why does apply with too many arguments throw "Maximum call stack size exceeded"?
问题描述
在 Chrome 和 Node 中,以下代码抛出错误:
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
我理解为什么将 100 万个参数传递给函数可能是个坏主意,但谁能解释为什么错误是 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?
(如果这看起来很无聊,原始案例是 Math.max.apply(Math, lotOfNumbers)
,这是从数组中获取最大数的一种合理方式.)
(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.)
推荐答案
函数参数放在堆栈上.您试图将一百万个参数放入堆栈,这超过了最大堆栈大小.所以错误信息与错误原因非常相关.
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.
这篇关于为什么带有太多参数的应用会抛出“超出最大调用堆栈大小"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!