Use Array instead. Arrays are generally faster for all other operations besides shifting and unshifting.
import { Vector } from "https://deno.land/x/collections@0.12.1/vector.ts";
A double-ended queue implemented with a growable ring buffer. Vector is faster than JavaScript's built in Array class for shifting and unshifting because it only requires reallocation when increasing the capacity.
Properties
The vector will be able to hold this many values without reallocating. If the length exceeds the capacity, then the capacity will be increased. Changing the capacity may trigger reallocation. Changing the capacity to less than the length will change the length to be equal to the new capacity.
Methods
Removes all values from the vector.
Merges two or more iterables together. This does not change existing Iterables, it returns a new Vector.
Removes and returns the value at index from the vector. If the value is negative, it will be subtracted from the end. The values between the index and the end will be shifted to the left.
Returns an iterator for retrieving and removing values from the vector (from left-to-right).
Returns an iterator for retrieving and removing values from the vector (from right-to-left).
Returns true if a value in the vector satisfies the provided testing function or false if it is not found. Optionally, you can search a subset of the vector by providing an index range. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector.
Returns the first value in the vector that satisfies the provided testing function or undefined if it is not found. Optionally, you can search a subset of the vector by providing an index range. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector.
Returns the index of the first value in the vector that satisfies the provided testing function or 1 if it is not found. Optionally, you can search a subset of the vector by providing an index range. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector.
Returns the last value in the vector that satisfies the provided testing function or undefined if it is not found. Optionally, you can search a subset of the vector by providing an index range. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector.
Returns the index of the last value in the vector that satisfies the provided testing function or -1 if it is not found. Optionally, you can search a subset of the vector by providing an index range. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector.
Executes the provided function once for each value in the vector. Optionally, you can iterate a subset of the vector by providing an index range. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector.
Returns the value at the given index. If the value is negative, it will be subtracted from the end. The index 0 would return the first value in the vector. The index -1 would return the last value in the vector.
Returns true if the search value can be found in the vector, or false if it is not found. This uses strict equality checks. Optionally, you can search a subset of the vector by providing an index range. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector.
Returns the first index at which the search value can be found in the vector, or -1 if it is not found. This uses strict equality checks. Optionally, you can search a subset of the vector by providing an index range. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector.
Checks if the vector is empty.
Creates and returns a new string concatenating all of the values in the Vector, converted to strings using their toString methods and separated by commas or a specified separator string.
Returns the last index at which the search value can be found in the vector, or -1 if it is not found. This uses strict equality checks. Optionally, you can search a subset of the vector by providing an index range. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector.
Creates a new vector from the results of executing the provided function for each value in the vector. Optionally, you can iterate a subset of the vector by providing an index range. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector.
Removes the last value from the vector and returns it, or undefined if it is empty.
Applies a function against an accumulator and each value of the vector (from left-to-right) to reduce it to a single value. If no initial value is supplied, the first value in the vector will be used and skipped. Calling reduce on an empty array without an initial value creates a TypeError.
Applies a function against an accumulator and each value of the vector (from right-to-left) to reduce it to a single value. If no initial value is supplied, the last value in the vector will be used and skipped. Calling reduce on an empty array without an initial value creates a TypeError.
Sets the value at the given index, then returns the value. If the value is negative, it will be subtracted from the end. The index 0 would set the first value in the vector. The index -1 would set the last value in the vector. If the absolute index value is greater than the length, the size will be increased to match before setting the value.
Removes the first value from the vector and returns it, or undefined if it is empty.
Shrinks the capacity to be equal to the length.
Returns a shallow copy of a portion of the vector into a new vector.
The start and end represent the index of values in the vector.
The end is exclusive meaning it will not be included.
If the index value is negative, it will be subtracted from the end of the vector.
For example, vector.slice(-2)
would return a new vector
containing the last 2 values.
Returns true if a value in the vector satisfies the provided testing function or false if it is not found. Optionally, you can search a subset of the vector by providing an index range. The start and end represent the index of values in the vector. The end is exclusive meaning it will not be included. If the index value is negative, it will be subtracted from the end of the vector.
Sorts the values of the vector in place then returns it. This uses Array sort method internally. If the vector has been shifted it may trigger reallocation before sorting.
Changes the contents of an array in place by removing or replacing existing elements or inserting new values. Then returns an Vector of the values that were removed. Returns a shallow copy of a portion of the vector into a new vector. The start represents the index of value you may insert values before or delete values starting from. The deleteCount is the number of values you would like to delete from the vector. The deleteCount would default to the number of values between the index and the end of the vector. If the start value is negative, it will be subtracted from the end of the vector. If the deleteCount is less than 0, no values will be deleted. If any insert values are specified, they will be inserted before the start index.
Converts the vector to an array.
It's recommended to use this instead of Array.from
because
this method is significantly faster.
Creates and returns a new string concatenating all of the values in the Vector, converted to strings using their toLocaleString methods and separated by a locale-specific string.
Creates and returns a new string concatenating all of the values in the Vector, converted to strings using their toString methods and separated by commas.
Returns an iterator for retrieving values from the vector (from left-to-right).
Returns an iterator for retrieving values from the vector (from right-to-left).
Returns an iterator for retrieving values from the vector (from left-to-right).