Most efficient way to create a zero filled JavaScr

2018-12-31 12:45发布

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

30条回答
春风洒进眼中
2楼-- · 2018-12-31 13:24

ES6 solution:

[...new Array(5)].map(x => 0); // [0, 0, 0, 0, 0]
查看更多
梦醉为红颜
3楼-- · 2018-12-31 13:24

This concat version is much faster in my tests on Chrome (2013-03-21). About 200ms for 10,000,000 elements vs 675 for straight init.

function filledArray(len, value) {
    if (len <= 0) return [];
    var result = [value];
    while (result.length < len/2) {
        result = result.concat(result);
    }
    return result.concat(result.slice(0, len-result.length));
}

Bonus: if you want to fill your array with Strings, this is a concise way to do it (not quite as fast as concat though):

function filledArrayString(len, value) {
    return new Array(len+1).join(value).split('');
}
查看更多
孤独总比滥情好
4楼-- · 2018-12-31 13:25
function zeroFilledArray(size) {
    return new Array(size + 1).join('0').split('');
}
查看更多
伤终究还是伤i
5楼-- · 2018-12-31 13:25

I just use :

var arr = [10];
for (var i=0; i<=arr.length;arr[i] = i, i++);
查看更多
怪性笑人.
6楼-- · 2018-12-31 13:26

The way I usually do it (and is amazing fast) is using Uint8Array. For example, creating a zero filled vector of 1M elements:

  var zeroFilled = [].slice.apply(new Uint8Array(1000000))

I'm a Linux user and always have worked for me, but once a friend using a Mac had some non-zero elements. I thought his machine was malfunctioning, but still here's the safest way we found to fix it:

  var zeroFilled = [].slice.apply(new Uint8Array(new Array(1000000)) 

Edited

Chrome 25.0.1364.160

  1. Frederik Gottlieb - 6.43
  2. Sam Barnum - 4.83
  3. Eli - 3.68
  4. Joshua 2.91
  5. Mathew Crumley - 2.67
  6. bduran - 2.55
  7. Allen Rice - 2.11
  8. kangax - 0.68
  9. Tj. Crowder - 0.67
  10. zertosh - ERROR

Firefox 20.0

  1. Allen Rice - 1.85
  2. Joshua - 1.82
  3. Mathew Crumley - 1.79
  4. bduran - 1.37
  5. Frederik Gottlieb - 0.67
  6. Sam Barnum - 0.63
  7. Eli - 0.59
  8. kagax - 0.13
  9. Tj. Crowder - 0.13
  10. zertosh - ERROR

Missing the most important test (at least for me): the Node.js one. I suspect it close to Chrome benchmark.

查看更多
临风纵饮
7楼-- · 2018-12-31 13:28

If you need to create many zero filled arrays of different lengths during the execution of your code, the fastest way I've found to achieve this is to create a zero array once, using one of the methods mentioned on this topic, of a length which you know will never be exceeded, and then slice that array as necessary.

For example (using the function from the chosen answer above to initialize the array), create a zero filled array of length maxLength, as a variable visible to the code that needs zero arrays:

var zero = newFilledArray(maxLength, 0);

Now slice this array everytime you need a zero filled array of length requiredLength < maxLength:

zero.slice(0, requiredLength);

I was creating zero filled arrays thousands of times during execution of my code, this speeded up the process tremendously.

查看更多
登录 后发表回答