constlength=1000000;letstart=performance.now();consta=newArray(length);letfinish=performance.now();console.log(`Sparse array was created in ${finish-start}ms`);// < 5msstart=performance.now();constb=Array.from({length});finish=performance.now();console.log(`Dense array was created in ${finish-start}ms`);// ~60msstart=performance.now();constc=[];c[length-1]=1;finish=performance.now();console.log(`Element added in ${finish-start}ms`);// < 1msconsole.log(c[length-1]);// 1console.log(c.length);// 1000000
Basically think of arrays as key-value stores that use positive integers as keys.
The problem is that JavaScript engines optimize arrays for performance
PACKED (dense) arrays can transition to HOLEY (sparse) arrays - but not the other way around
PACKED processing is more optimized, though:
the performance difference between accessing holey or packed arrays is usually too small to matter or even be measurable.
That said (as already suggested elsewhere), if holes don't have any particular meaning within the processing context, it's better to sanitize HOLEY arrays into PACKED arrays rather than relying on the "skipping behaviour" of select methods.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Basically think of arrays as key-value stores that use positive integers as keys.
The problem is that JavaScript engines optimize arrays for performance
v8.dev: Elements kinds in V8:
PACKED_SMI_ELEMENTS->HOLEY_SMI_ELEMENTSPACKED_DOUBLE_ELEMENTS->HOLEY_DOUBLE_ELEMENTSPACKED_ELEMENTS->HOLEY_ELEMENTS.PACKED(dense) arrays can transition toHOLEY(sparse) arrays - but not the other way aroundPACKEDprocessing is more optimized, though:That said (as already suggested elsewhere), if holes don't have any particular meaning within the processing context, it's better to sanitize
HOLEYarrays intoPACKEDarrays rather than relying on the "skipping behaviour" of select methods.