array dynamic length vs array preallocated

Edit on GitHub

Compare populating pre-sized array compared to an empty array that grows dynamically

Setup

const len = 1000000;

Tests

  • array dynamic length

    var arr1 = [];
    for (let i = 0; i < len; i++) {
    	arr1[i]=i;
    }
  • array preallocated

    var arr2 = new Array(len)
    for (let i = 0; i < len; i++) {
    	arr2[i]=i;
    }