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.

const vec = new Vector(Float64Array);

Then, you can push() and pop() entries from the Vector.

vec.push(1/5);
vec.push(2/5);
vec.push(3/5);

let top = vec.pop();
console.log(top); // 0.6

You can use any of the TypedArray types for the backing store of the Vector:

const octetVector = new Vector(Uint8Array, 128);
const clampedOctetVector = new Vector(Uint8ClampedArray, 128);
const shortVector = new Vector(Int16Array, 128);
const ushortVector = new Vector(Uint16Array, 128);
const longVector = new Vector(Int32Array, 128);
const ulongVector = new Vector(Uint32Array, 128);
const float32Vector = new Vector(Float32Array, 128);

Type Parameters

Constructors

  • Create an empty Vector with a given capacity, the default is 0.

    Type Parameters

    Parameters

    Returns Vector<T>

  • You can also customize more options, by using VectorOptions in the second argument.

    Type Parameters

    Parameters

    Returns Vector<T>

    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 },
    );

Accessors

  • get capacity(): number
  • Current the current length of the backing TypedArray. This is the maximum number of elements this Vector can hold before it will be resized.

    Returns number

Methods

  • Set the value at the given index.

    Parameters

    • index: number

      The index of the element to set.

    • value: number

      The value of the element.

    Returns void

    Error if the index is out of range.

  • Retrieves the element at the given index.

    Parameters

    • index: number

      The index of the element to get.

    Returns number

    Error if the index is out of range.

  • Set multiple values starting at begin.

    Parameters

    • begin: number

      The index to start at.

    • values: ArrayLike<number>

      The values to set.

    Returns void

  • Push a single element onto the end of the Vector, resizing if needed.

    Parameters

    • Rest...args: number[]

      The values to push to the end of the vector.

    Returns number

    The new length of the vector.

  • Push multiple elements onto the end of the Vector, resizing multiple-times if needed.

    Parameters

    • values: ArrayLike<number>

    Returns number

    The new length of the vector.

  • Remove and return a single element from the end of the Vector.

    Returns undefined | number

    The removed value, unless the Vector is empty, then undefined.

  • Remove and return a multiple elements from the end of the Vector.

    Parameters

    • count: number

    Returns T

    A TypedArray of the removed values.

    const vec = Vector.from([1, 2, 3, 4, 5], Uint8Array);
    const three = vec.popN(3);
  • Parameters

    • count: number
    • dest: T

    Returns T

  • Manually trigger the Vector to grow in size, using the growth behavior.

    Returns this

  • Reallocate the underlying TypedArray.

    If the capacity is less-than the current length, then the extra values are discarded.

    Parameters

    • capacity: number = ...

      The desired capacity of the Vector. Defaults to the current length of the Vector.

    Returns this

  • 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.

    Returns IterableIterator<number>

    const vec = Vector.from([1, 2, 3], Uint8Array);
    for (const value of vec.consume()) {
    console.log(value); // 3, 2, 1
    }

    console.log(vec.length); // 0.