Extremely fast double-ended queue implementation

Extremely fast double-ended queue implementation. Double-ended queue can also be used as a:

The implementation is GC and CPU cache friendly circular buffer. It will run circles around any "linked list" implementation.

Every queue operation is done in constant `O(1)`

- including random access from `.get()`

.

- Quick start
- Why not use an Array?
- Using double-ended queue as a normal queue
- API reference and examples
- Performance

```
npm install double-ended-queue
```

`var Deque = ; var deque = 1234;deque; //1 deque; //4 `

Arrays take linear `O(N)`

time to do `shift`

and `unshift`

operations. That means in theory that an array with 1000 items is 1000x slower to do those operations than a deque with 1000 items. 10000x slower with 10000 items and so on.

V8 implements a trick for small arrays where these operations are done in constant time, however even with this trick deque is still 4x faster.

But arrays use "native" methods, they must be faster!

In V8, there is almost no advantage for a method to be a built-in. In fact many times built-ins are at a severe disadvantage of having to implement far more complex semantics than is actually needed in practice. For example, sparse array handling punishes almost every built-in array method even though nobody uses sparse arrays as is evidenced by the popularity of the underscore library which doesn't handle sparse arrays in the same way across different browsers.

Queue is a more commonly needed data structure however a separate implementation does not provide any advantage in terms of performance. Aliases are provided specifically for the queue use-case. You may use `.enqueue(items...)`

to enqueue item(s) and `.dequeue()`

to dequeue an item.

`new Deque()`

`new Deque(Array items)`

`new Deque(int capacity)`

`push(dynamic items...)`

`unshift(dynamic items...)`

`pop()`

`shift()`

`toArray()`

`peekBack()`

`peekFront()`

`get(int index)`

`isEmpty()`

`clear()`

`new Deque()`

-> `Deque`

Creates an empty double-ended queue with initial capacity of 16. If you know the optimal size before-hand, use `new Deque(int capacity)`

.

`var deque = ;deque;deque; //1 deque; //3 `

`new Deque(Array items)`

-> `Deque`

Creates a double-ended queue from `items`

.

`var deque = 1234;deque; //1 deque; //4 `

`new Deque(int capacity)`

-> `Deque`

Creates an empty double-ended queue with the given `capacity`

. `Capacity`

should be the maximum amount of items the queue will hold at a given time.

The reason to give an initial capacity is to avoid potentially expensive resizing operations at runtime.

`var deque = 100;deque;deque; //1 deque; //3 `

`push(dynamic items...)`

-> `int`

Push items to the back of this queue. Returns the amount of items currently in the queue after the operation.

`var deque = ;deque;deque; //1 deque;deque; //1 deque; //2 deque; //3 `

**Aliases:** `enqueue`

, `insertBack`

`unshift(dynamic items...)`

-> `int`

Unshift items to the front of this queue. Returns the amount of items currently in the queue after the operation.

`var deque = 23;deque;deque; //"1,2,3" deque;deque; //"-2,-1,0,1,2,3" `

**Aliases:** `insertFront`

`pop()`

-> `dynamic`

Pop off the item at the back of this queue.

Note: The item will be removed from the queue. If you simply want to see what's at the back of the queue use `peekBack()`

or `.get(-1)`

.

If the queue is empty, `undefined`

is returned. If you need to differentiate between `undefined`

values in the queue and `pop()`

return value -
check the queue `.length`

before popping.

`var deque = 123;deque; //3 deque; //2 deque; //1 deque; //undefined `

**Aliases:** `removeBack`

`shift()`

-> `dynamic`

Shifts off the item at the front of this queue.

Note: The item will be removed from the queue. If you simply want to see what's at the front of the queue use `peekFront()`

or `.get(0)`

.

If the queue is empty, `undefined`

is returned. If you need to differentiate between `undefined`

values in the queue and `shift()`

return value -
check the queue `.length`

before shifting.

`var deque = 123;deque; //1 deque; //2 deque; //3 deque; //undefined `

**Aliases:** `removeFront`

, `dequeue`

`toArray()`

-> `Array`

Returns the items in the queue as an array. Starting from the item in the front of the queue and ending to the item at the back of the queue.

`var deque = 123;deque;deque;deque; //[0,1,2,3,4] `

**Aliases:** `toJSON`

`peekBack()`

-> `dynamic`

Returns the item that is at the back of this queue without removing it.

If the queue is empty, `undefined`

is returned.

`var deque = 123;deque;deque; //4 `

`peekFront()`

-> `dynamic`

Returns the item that is at the front of this queue without removing it.

If the queue is empty, `undefined`

is returned.

`var deque = 123;deque;deque; //1 `

`get(int index)`

-> `dynamic`

Returns the item that is at the given `index`

of this queue without removing it.

The index is zero-based, so `.get(0)`

will return the item that is at the front, `.get(1)`

will return
the item that comes after and so on.

The index can be negative to read items at the back of the queue. `.get(-1)`

returns the item that is at the back of the queue,
`.get(-2)`

will return the item that comes before and so on.

Returns `undefined`

if `index`

is not a valid index into the queue.

`var deque = 123;deque; //1 deque; //2 deque; //3 deque; //3 deque; //2 deque; //1 `

**Note**: Even though indexed accessor (e.g. `queue[0]`

) could *appear* to return a correct value *sometimes*, this is completely unreliable. The numeric slots
of the deque object are internally used as an optimization and have no meaningful order or meaning to outside. Always use `.get()`

.

**Note**: The implementation has O(1) random access using `.get()`

.

`isEmpty()`

-> `boolean`

Return `true`

if this queue is empty, `false`

otherwise.

`var deque = ;deque; //true deque;deque; //false `

`clear()`

-> `void`

Remove all items from this queue. Does not change the queue's capacity.

`var deque = 123;deque; //"1,2,3" dequeclear;deque; //"" `

Clone the repo and `npm install`

. Then run the `bench`

script.

```
double-ended-queue x 15,532,714 ops/sec ±0.19% (96 runs sampled)
built-in array x 6,501,398 ops/sec ±0.87% (95 runs sampled)
node-deque x 2,938,068 ops/sec ±3.50% (68 runs sampled)
```

```
double-ended-queue x 14,425,547 ops/sec ±0.17% (94 runs sampled)
node-deque x 2,815,628 ops/sec ±10.56% (76 runs sampled)
built-in array x 19.23 ops/sec ±0.35% (51 runs sampled)
```

Noteworthy is just how bad the degradation can be for built-in array when V8 cannot use the trick.