You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 9.4KB

hace 4 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <p align="center">
  2. <a href="https://invertase.io">
  3. <img src="https://static.invertase.io/assets/invertase-logo-small.png"><br/>
  4. </a>
  5. <h2 align="center">Denque</h2>
  6. </p>
  7. <p align="center">
  8. <a href="https://www.npmjs.com/package/denque"><img src="https://img.shields.io/npm/dm/denque.svg?style=flat-square" alt="NPM downloads"></a>
  9. <a href="https://www.npmjs.com/package/denque"><img src="https://img.shields.io/npm/v/denque.svg?style=flat-square" alt="NPM version"></a>
  10. <a href="https://travis-ci.org/Salakar/denque"><img src="https://travis-ci.org/invertase/denque.svg" alt="Build version"></a>
  11. <a href="https://coveralls.io/github/invertase/denque?branch=master"><img src="https://coveralls.io/repos/github/invertase/denque/badge.svg?branch=master" alt="Build version"></a>
  12. <a href="/LICENSE"><img src="https://img.shields.io/npm/l/denque.svg?style=flat-square" alt="License"></a>
  13. <a href="https://discord.gg/C9aK28N"><img src="https://img.shields.io/discord/295953187817521152.svg?logo=discord&style=flat-square&colorA=7289da&label=discord" alt="Chat"></a>
  14. <a href="https://twitter.com/invertaseio"><img src="https://img.shields.io/twitter/follow/invertaseio.svg?style=social&label=Follow" alt="Follow on Twitter"></a>
  15. </p>
  16. Extremely fast and lightweight [double-ended queue](http://en.wikipedia.org/wiki/Double-ended_queue) implementation with zero dependencies.
  17. Double-ended queues can also be used as a:
  18. - [Stack](http://en.wikipedia.org/wiki/Stack_\(abstract_data_type\))
  19. - [Queue](http://en.wikipedia.org/wiki/Queue_\(data_structure\))
  20. This implementation is currently the fastest available, even faster than `double-ended-queue`, see the [benchmarks](#benchmarks)
  21. Every queue operation is done at a constant `O(1)` - including random access from `.peekAt(index)`.
  22. **Works on all node versions >= v0.10**
  23. # Quick Start
  24. npm install denque
  25. ```js
  26. const Denque = require("denque");
  27. const denque = new Denque([1,2,3,4]);
  28. denque.shift(); // 1
  29. denque.pop(); // 4
  30. ```
  31. # API
  32. - [`new Denque()`](#new-denque---denque)
  33. - [`new Denque(Array items)`](#new-denquearray-items---denque)
  34. - [`push(item)`](#pushitem---int)
  35. - [`unshift(item)`](#unshiftitem---int)
  36. - [`pop()`](#pop---dynamic)
  37. - [`shift()`](#shift---dynamic)
  38. - [`toArray()`](#toarray---array)
  39. - [`peekBack()`](#peekback---dynamic)
  40. - [`peekFront()`](#peekfront---dynamic)
  41. - [`peekAt(int index)`](#peekAtint-index---dynamic)
  42. - [`remove(int index, int count)`](#remove)
  43. - [`removeOne(int index)`](#removeOne)
  44. - [`splice(int index, int count, item1, item2, ...)`](#splice)
  45. - [`isEmpty()`](#isempty---boolean)
  46. - [`clear()`](#clear---void)
  47. #### `new Denque()` -> `Denque`
  48. Creates an empty double-ended queue with initial capacity of 4.
  49. ```js
  50. var denque = new Denque();
  51. denque.push(1);
  52. denque.push(2);
  53. denque.push(3);
  54. denque.shift(); //1
  55. denque.pop(); //3
  56. ```
  57. <hr>
  58. #### `new Denque(Array items)` -> `Denque`
  59. Creates a double-ended queue from `items`.
  60. ```js
  61. var denque = new Denque([1,2,3,4]);
  62. denque.shift(); // 1
  63. denque.pop(); // 4
  64. ```
  65. <hr>
  66. #### `push(item)` -> `int`
  67. Push an item to the back of this queue. Returns the amount of items currently in the queue after the operation.
  68. ```js
  69. var denque = new Denque();
  70. denque.push(1);
  71. denque.pop(); // 1
  72. denque.push(2);
  73. denque.push(3);
  74. denque.shift(); // 2
  75. denque.shift(); // 3
  76. ```
  77. <hr>
  78. #### `unshift(item)` -> `int`
  79. Unshift an item to the front of this queue. Returns the amount of items currently in the queue after the operation.
  80. ```js
  81. var denque = new Denque([2,3]);
  82. denque.unshift(1);
  83. denque.toString(); // "1,2,3"
  84. denque.unshift(-2);
  85. denque.toString(); // "-2,-1,0,1,2,3"
  86. ```
  87. <hr>
  88. #### `pop()` -> `dynamic`
  89. Pop off the item at the back of this queue.
  90. 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 [`.peekAt(-1)`](#peekAtint-index---dynamic).
  91. If the queue is empty, `undefined` is returned. If you need to differentiate between `undefined` values in the queue and `pop()` return value -
  92. check the queue `.length` before popping.
  93. ```js
  94. var denque = new Denque([1,2,3]);
  95. denque.pop(); // 3
  96. denque.pop(); // 2
  97. denque.pop(); // 1
  98. denque.pop(); // undefined
  99. ```
  100. **Aliases:** `removeBack`
  101. <hr>
  102. #### `shift()` -> `dynamic`
  103. Shifts off the item at the front of this queue.
  104. 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 [`.peekAt(0)`](#peekAtint-index---dynamic).
  105. If the queue is empty, `undefined` is returned. If you need to differentiate between `undefined` values in the queue and `shift()` return value -
  106. check the queue `.length` before shifting.
  107. ```js
  108. var denque = new Denque([1,2,3]);
  109. denque.shift(); // 1
  110. denque.shift(); // 2
  111. denque.shift(); // 3
  112. denque.shift(); // undefined
  113. ```
  114. <hr>
  115. #### `toArray()` -> `Array`
  116. 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.
  117. ```js
  118. var denque = new Denque([1,2,3]);
  119. denque.push(4);
  120. denque.unshift(0);
  121. denque.toArray(); // [0,1,2,3,4]
  122. ```
  123. <hr>
  124. #### `peekBack()` -> `dynamic`
  125. Returns the item that is at the back of this queue without removing it.
  126. If the queue is empty, `undefined` is returned.
  127. ```js
  128. var denque = new Denque([1,2,3]);
  129. denque.push(4);
  130. denque.peekBack(); // 4
  131. ```
  132. <hr>
  133. #### `peekFront()` -> `dynamic`
  134. Returns the item that is at the front of this queue without removing it.
  135. If the queue is empty, `undefined` is returned.
  136. ```js
  137. var denque = new Denque([1,2,3]);
  138. denque.push(4);
  139. denque.peekFront(); // 1
  140. ```
  141. <hr>
  142. #### `peekAt(int index)` -> `dynamic`
  143. Returns the item that is at the given `index` of this queue without removing it.
  144. The index is zero-based, so `.peekAt(0)` will return the item that is at the front, `.peekAt(1)` will return
  145. the item that comes after and so on.
  146. The index can be negative to read items at the back of the queue. `.peekAt(-1)` returns the item that is at the back of the queue,
  147. `.peekAt(-2)` will return the item that comes before and so on.
  148. Returns `undefined` if `index` is not a valid index into the queue.
  149. ```js
  150. var denque = new Denque([1,2,3]);
  151. denque.peekAt(0); //1
  152. denque.peekAt(1); //2
  153. denque.peekAt(2); //3
  154. denque.peekAt(-1); // 3
  155. denque.peekAt(-2); // 2
  156. denque.peekAt(-3); // 1
  157. ```
  158. **Note**: The implementation has O(1) random access using `.peekAt()`.
  159. **Aliases:** `get`
  160. <hr>
  161. #### `remove(int index, int count)` -> `array`
  162. Remove number of items from the specified index from the list.
  163. Returns array of removed items.
  164. Returns undefined if the list is empty.
  165. ```js
  166. var denque = new Denque([1,2,3,4,5,6,7]);
  167. denque.remove(0,3); //[1,2,3]
  168. denque.remove(1,2); //[5,6]
  169. var denque1 = new Denque([1,2,3,4,5,6,7]);
  170. denque1.remove(4, 100); //[5,6,7]
  171. ```
  172. <hr>
  173. #### `removeOne(int index)` -> `dynamic`
  174. Remove and return the item at the specified index from the list.
  175. Returns undefined if the list is empty.
  176. ```js
  177. var denque = new Denque([1,2,3,4,5,6,7]);
  178. denque.removeOne(4); // 5
  179. denque.removeOne(3); // 4
  180. denque1.removeOne(1); // 2
  181. ```
  182. <hr>
  183. #### `splice(int index, int count, item1, item2, ...)` -> `array`
  184. Native splice implementation.
  185. Remove number of items from the specified index from the list and/or add new elements.
  186. Returns array of removed items or empty array if count == 0.
  187. Returns undefined if the list is empty.
  188. ```js
  189. var denque = new Denque([1,2,3,4,5,6,7]);
  190. denque.splice(denque.length, 0, 8, 9, 10); // []
  191. denque.toArray() // [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
  192. denque.splice(3, 3, 44, 55, 66); // [4,5,6]
  193. denque.splice(5,4, 666,667,668,669); // [ 66, 7, 8, 9 ]
  194. denque.toArray() // [ 1, 2, 3, 44, 55, 666, 667, 668, 669, 10 ]
  195. ```
  196. <hr>
  197. #### `isEmpty()` -> `boolean`
  198. Return `true` if this queue is empty, `false` otherwise.
  199. ```js
  200. var denque = new Denque();
  201. denque.isEmpty(); // true
  202. denque.push(1);
  203. denque.isEmpty(); // false
  204. ```
  205. <hr>
  206. #### `clear()` -> `void`
  207. Remove all items from this queue. Does not change the queue's capacity.
  208. ```js
  209. var denque = new Denque([1,2,3]);
  210. denque.toString(); // "1,2,3"
  211. denque.clear();
  212. denque.toString(); // ""
  213. ```
  214. <hr>
  215. ## Benchmarks
  216. #### Platform info:
  217. ```
  218. Darwin 17.0.0 x64
  219. Node.JS 9.4.0
  220. V8 6.2.414.46-node.17
  221. Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz × 8
  222. ```
  223. #### 1000 items in queue
  224. (3 x shift + 3 x push ops per 'op')
  225. denque x 64,365,425 ops/sec ±0.69% (92 runs sampled)
  226. double-ended-queue x 26,646,882 ops/sec ±0.47% (94 runs sampled)
  227. #### 2 million items in queue
  228. (3 x shift + 3 x push ops per 'op')
  229. denque x 61,994,249 ops/sec ±0.26% (95 runs sampled)
  230. double-ended-queue x 26,363,500 ops/sec ±0.42% (91 runs sampled)
  231. #### Splice
  232. (1 x splice per 'op') - initial size of 100,000 items
  233. denque.splice x 925,749 ops/sec ±22.29% (77 runs sampled)
  234. native array splice x 7,777 ops/sec ±8.35% (50 runs sampled)
  235. #### Remove
  236. (1 x remove + 10 x push per 'op') - initial size of 100,000 items
  237. denque.remove x 2,635,275 ops/sec ±0.37% (95 runs sampled)
  238. native array splice - Fails to complete: "JavaScript heap out of memory"
  239. #### Remove One
  240. (1 x removeOne + 10 x push per 'op') - initial size of 100,000 items
  241. denque.removeOne x 1,088,240 ops/sec ±0.21% (93 runs sampled)
  242. native array splice x 5,300 ops/sec ±0.41% (96 runs sampled)
  243. ---
  244. Built and maintained with 💛 by [Invertase](https://invertase.io).
  245. - [💼 Hire Us](https://invertase.io/hire-us)
  246. - [☕️ Sponsor Us](https://opencollective.com/react-native-firebase)
  247. - [👩‍💻 Work With Us](https://invertase.io/jobs)