README.md 8.33 KB
Newer Older
Adam Procter's avatar
Adam Procter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
#Introduction

Extremely fast [double-ended queue](http://en.wikipedia.org/wiki/Double-ended_queue) implementation. Double-ended queue can also be used as a:

- [Stack](http://en.wikipedia.org/wiki/Stack_\(abstract_data_type\))
- [Queue](http://en.wikipedia.org/wiki/Queue_\(data_structure\))

The implementation is GC and CPU cache friendly [circular buffer](http://en.wikipedia.org/wiki/Circular_buffer). [It will run circles around any "linked list" implementation](#performance).

Every queue operation is done in constant `O(1)` - including random access from `.get()`.

#Topics

- [Quick start](#quick-start)
- [Why not use an Array?](#why-not-use-an-array)
- [Using double-ended queue as a normal queue](#using-double-ended-queue-as-a-normal-queue)
- [API reference and examples](#api)
- [Performance](#performance)

#Quick start

    npm install double-ended-queue

```js
var Deque = require("double-ended-queue");

var deque = new Deque([1,2,3,4]);
deque.shift(); //1
deque.pop(); //4
```

#Why not use an Array?

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](https://code.google.com/p/v8/issues/detail?id=3059) 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.

#Using double-ended queue as a normal queue

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.

#API

- [`new Deque()`](#new-deque---deque)
- [`new Deque(Array items)`](#new-dequearray-items---deque)
- [`new Deque(int capacity)`](#new-dequeint-capacity---deque)
- [`push(dynamic items...)`](#pushdynamic-items---int)
- [`unshift(dynamic items...)`](#unshiftdynamic-items---int)
- [`pop()`](#pop---dynamic)
- [`shift()`](#shift---dynamic)
- [`toArray()`](#toarray---array)
- [`peekBack()`](#peekback---dynamic)
- [`peekFront()`](#peekfront---dynamic)
- [`get(int index)`](#getint-index---dynamic)
- [`isEmpty()`](#isempty---boolean)
- [`clear()`](#clear---void)

#####`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)`](#new-dequeint-capacity---deque).

```js
var deque = new Deque();
deque.push(1, 2, 3);
deque.shift(); //1
deque.pop(); //3
```

<hr>

#####`new Deque(Array items)` -> `Deque`

Creates a double-ended queue from `items`.

```js
var deque = new Deque([1,2,3,4]);
deque.shift(); //1
deque.pop(); //4
```

<hr>

#####`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.

```js
var deque = new Deque(100);
deque.push(1, 2, 3);
deque.shift(); //1
deque.pop(); //3
```

<hr>

#####`push(dynamic items...)` -> `int`

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

```js
var deque = new Deque();
deque.push(1);
deque.pop(); //1
deque.push(1, 2, 3);
deque.shift(); //1
deque.shift(); //2
deque.shift(); //3
```

**Aliases:** `enqueue`, `insertBack`

<hr>

#####`unshift(dynamic items...)` -> `int`

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

```js
var deque = new Deque([2,3]);
deque.unshift(1);
deque.toString(); //"1,2,3"
deque.unshift(-2, -1, 0);
deque.toString(); //"-2,-1,0,1,2,3"
```

**Aliases:** `insertFront`

<hr>


#####`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()`](#peekback---dynamic) or [`.get(-1)`](#getint-index---dynamic).

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.

```js
var deque = new Deque([1,2,3]);
deque.pop(); //3
deque.pop(); //2
deque.pop(); //1
deque.pop(); //undefined
```

**Aliases:** `removeBack`

<hr>

#####`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()`](#peekfront---dynamic) or [`.get(0)`](#getint-index---dynamic).

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.

```js
var deque = new Deque([1,2,3]);
deque.shift(); //1
deque.shift(); //2
deque.shift(); //3
deque.shift(); //undefined
```

**Aliases:** `removeFront`, `dequeue`

<hr>

#####`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.

```js
var deque = new Deque([1,2,3]);
deque.push(4);
deque.unshift(0);
deque.toArray(); //[0,1,2,3,4]
```

**Aliases:** `toJSON`

<hr>

#####`peekBack()` -> `dynamic`

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

If the queue is empty, `undefined` is returned.

```js
var deque = new Deque([1,2,3]);
deque.push(4);
deque.peekBack(); //4
```

<hr>

#####`peekFront()` -> `dynamic`

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

If the queue is empty, `undefined` is returned.

```js
var deque = new Deque([1,2,3]);
deque.push(4);
deque.peekFront(); //1
```

<hr>

#####`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.

```js
var deque = new Deque([1,2,3]);
deque.get(0); //1
deque.get(1); //2
deque.get(2); //3

deque.get(-1); //3
deque.get(-2); //2
deque.get(-3); //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()`.

<hr>

#####`isEmpty()` -> `boolean`

Return `true` if this queue is empty, `false` otherwise.

```js
var deque = new Deque();
deque.isEmpty(); //true
deque.push(1);
deque.isEmpty(); //false
```

<hr>

#####`clear()` -> `void`

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

```js
var deque = new Deque([1,2,3]);
deque.toString(); //"1,2,3"
deque.clear();
deque.toString(); //""
```
<hr>

#Performance

Clone the repo and `npm install`. Then run the `bench` script.

##1000 items in the queue

    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)

##2 million items in the queue

    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.