Some gotchas: new Array and Array.from behave differently:
- Input Source:
new Array()requires explicit values or a length integer.Array.from()accepts any iterable or array-like object (e.g.,{ length: 5 },Set,NodeList). - Sparse Arrays:
new Array(5)creates a sparse array with undefined holes.Array.from({ length: 5 })creates a dense array filled withundefinedvalues, which allows methods like.map()to iterate over all indices. - Transformation:
new Array()has no built-in transformation.Array.from()supports a secondmapFnargument for on-the-fly element modification. - Performance: Benchmarks indicate that
new Array()is significantly faster for simple creation tasks, whereasArray.from()involves more overhead due to its flexibility and mapping capabilities.
Apart from the standard array, one can use Int16Array or https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array