1.2.2 • Published 4 years ago
packed-array v1.2.2
packed-array
Social Media Photo by Elevate on Unsplash
An utility to create fast, non-holey arrays.
import packedArray from 'packed-array';
const packedArray = require('packed-array');
// via unpkg, as global utility
// <script src="https://unpkg.com/packed-array">
API
This utility has the exact same Array.from API, except the first parameter can be an integer, as length
, and the second one can be just a static value, to fill the Array of specified length
with such value.
const array = packedArray(
length, // either a number, or an object/array with a {length}
fill, // either a static value, or a function used via Array.from
thisArg // optionally, if `fill` is a function, it will be passed as context
);
Goal
Simplify the creation of arrays extremely fast to be looped or consumed within their length
boundaries.
Native Alternative
If you're after just arrays with a length and some initial value, the following would likely perform better, producing the same result:
// note the Array(length) is mandatory to obtain a packed kind
const array = Array.from(Array(length), () => value);