Most efficient way to create a zero filled JavaScript array?

前端 未结 30 1194
花落未央
花落未央 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:14

    In short

    Fastest solution

    let a = new Array(n); for (let i=0; i

    Shortest (handy) solution (3x slower for small arrays, slightly slower for big (slowest on Firefox))

    Array(n).fill(0)


    Details

    Today 2020.06.09 I perform tests on macOS High Sierra 10.13.6 on browsers Chrome 83.0, Firefox 77.0, and Safari 13.1. I test chosen solutions for two test cases

    • small array - with 10 elements - you can perform test HERE
    • big arrays - with 1M elements - you can perform test HERE

    Conclusions

    • solution based on new Array(n)+for (N) is fastest solution for small arrays and big arrays (except Chrome but still very fast there) and it is recommended as fast cross-browser solution
    • solution based on new Float32Array(n) (I) returns non typical array (e.g. you cannot call push(..) on it) so I not compare its results with other solutions - however this solution is about 10-20x faster than other solutions for big arrays on all browsers
    • solutions based on for (L,M,N,O) are fast for small arrays
    • solutions based on fill (B,C) are fast on Chrome and Safari but surprisingly slowest on Firefox for big arrays. They are medium fast for small arrays
    • solution based on Array.apply (P) throws error for big arrays
      function P(n) {
        return Array.apply(null, Array(n)).map(Number.prototype.valueOf,0);
      }
      
      
      try {
        P(1000000);
      } catch(e) { 
        console.error(e.message);
      }

    Code and example

    Below code presents solutions used in measurements

    function A(n) {
      return [...new Array(n)].fill(0);
    }
    
    function B(n) {
      return new Array(n).fill(0);
    }
    
    function C(n) {
      return Array(n).fill(0);
    }
    
    function D(n) {
      return Array.from({length: n}, () => 0);
    }
    
    function E(n) {
      return [...new Array(n)].map(x => 0);
    }
    
    // arrays with type
    
    function F(n) {
      return Array.from(new Int32Array(n));
    }
    
    function G(n) {
      return Array.from(new Float32Array(n));
    }
    
    function H(n) {
      return Array.from(new Float64Array(n)); // needs 2x more memory than float32
    }
    
    function I(n) {
      return new Float32Array(n); // this is not typical array
    }
    
    function J(n) {
      return [].slice.apply(new Float32Array(n));
    }
    
    // Based on for
    
    function K(n) {
      let a = [];
      a.length = n;
      let i = 0;
      while (i < n) {
        a[i] = 0;
        i++;
      }
      return a;
    }
    
    function L(n) {
      let a=[]; for(let i=0; i {
      let a = f(10); 
      console.log(`${f.name} length=${a.length}, arr[0]=${a[0]}, arr[9]=${a[9]}`)
    });
    This snippets only present used codes

    Example results for Chrome

提交回复
热议问题