Most efficient way to create a zero filled JavaScript array?

前端 未结 30 1611
花落未央
花落未央 2020-11-22 05:58

What is the most efficient way to create an arbitrary length zero filled array in JavaScript?

30条回答
  •  迷失自我
    2020-11-22 06:10

    ES6 introduces Array.prototype.fill. It can be used like this:

    new Array(len).fill(0);
    

    Not sure if it's fast, but I like it because it's short and self-describing.

    It's still not in IE (check compatibility), but there's a polyfill available.

提交回复
热议问题