“RangeError: Maximum call stack size exceeded” Why?

后端 未结 4 419
攒了一身酷
攒了一身酷 2020-12-02 14:59

If I run

Array.apply(null, new Array(1000000)).map(Math.random);

on Chrome 33, I get

RangeError: Maximum call

4条回答
  •  臣服心动
    2020-12-02 15:34

    Browsers can't handle that many arguments. See this snippet for example:

    alert.apply(window, new Array(1000000000));
    

    This yields RangeError: Maximum call stack size exceeded which is the same as in your problem.

    To solve that, do:

    var arr = [];
    for(var i = 0; i < 1000000; i++){
        arr.push(Math.random());
    }
    

提交回复
热议问题