spread operator vs array.concat()

后端 未结 6 887
日久生厌
日久生厌 2020-12-01 04:00

What is the difference between spread operator and array.concat()

6条回答
  •  执笔经年
    2020-12-01 04:52

    The one difference I think is valid is that using spread operator for large array size will give you error of Maximum call stack size exceeded which you can avoid using the concat operator.

    var  someArray = new Array(600000);
    var newArray = [];
    var tempArray = [];
    
    
    someArray.fill("foo");
    
    try {
      newArray.push(...someArray);
    } catch (e) {
      console.log("Using spread operator:", e.message)
    }
    
    tempArray = newArray.concat(someArray);
    console.log("Using concat function:", tempArray.length)

提交回复
热议问题