Create an empty Vector with a given capacity, the default is 0
.
The constructor of the underlying backing TypedArray type.
Optional
initialCapacity: numberThe initial capacity of the Vector. Defaults to 0
.
You can also customize more options, by using VectorOptions in the second argument.
The constructor of the underlying backing TypedArray type.
Initial options
For instance, create a Vector that has an initial size of 10
, and a capacity of 100
:
const vec = new Vector(
Float32Array,
{
initialLength: 10,
initialCapacity: 100,
},
);
const vec = new Vector(
Uint32Array,
{ growthFn: (prev) => prev + 100 },
);
Current the current length of the backing TypedArray. This is the maximum number of elements this Vector can hold before it will be resized.
Get the current length of the Vector, will always been less-than-or-equal to Vector.capacity.
Set the value at the given index.
The index of the element to set.
The value of the element.
Error if the index is out of range.
Retrieves the element at the given index.
The index of the element to get.
Error if the index is out of range.
Push a single element onto the end of the Vector, resizing if needed.
Rest
...args: number[]The values to push to the end of the vector.
The new length of the vector.
Push multiple elements onto the end of the Vector, resizing multiple-times if needed.
The new length of the vector.
Manually trigger the Vector to grow in size, using the growth behavior.
Reallocate the underlying TypedArray.
If the capacity is less-than the current length, then the extra values are discarded.
Clear all entries from the Vector
Iterate through the vector from the tail-end, consuming each entry. Essentially, the same as running Vector.pop in a loop until Vector.length is zero.
Static
fromThe constructor of the underlying backing TypedArray type.
Optional
initialCapacity: numberThe initial capacity of the constructed Vector
he constructor of the underlying backing TypedArray type.
he options of constructed Vector
The Vector is a resizable data-structure that is backed by a TypedArray. Unlike a TypedArray—which are constructed with a fixed size—and JavaScript's built-in Array type—which are dynamically sized but are not as memory-efficient—the Vector strikes a balance between those two. However, this added functionality doesn't come without cost; depending on the use-case, the Vector can be outperformed by either a TypedArray or traditional a JavaScript Array. However, there are cases where using a Vector is preferable.
Example: A 64-bit floating point vector.
Then, you can
push()
andpop()
entries from the Vector.Example: Using other TypedArray types
You can use any of the TypedArray types for the backing store of the Vector: