https://github.com/creationix/fastqueue
A fast push/shift sync queue
https://github.com/creationix/fastqueue
Last synced: about 1 year ago
JSON representation
A fast push/shift sync queue
- Host: GitHub
- URL: https://github.com/creationix/fastqueue
- Owner: creationix
- License: mit
- Created: 2013-04-02T19:53:23.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2021-10-18T06:48:53.000Z (over 4 years ago)
- Last Synced: 2024-10-19T04:07:03.708Z (over 1 year ago)
- Language: JavaScript
- Size: 104 KB
- Stars: 29
- Watchers: 3
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fast Queue
While normal JavaScript arrays can be used as FIFO queues, the `.shift()` call is very slow if the queue gets large because it has to reindex all the remaining items on every shift.
This library is a fast queue that only implements `.push(item)`, `.shift()`, `.unshift(item)` and `.length` from the Array interface.
Internally it uses two arrays and cycles them and uses counters so that the `.shift()` calls are still fast.
## Usage
```js
var Queue = require('fastqueue');
var q = new Queue;
q.push(1);
q.push(2);
q.push(3);
var i = 4;
while (q.length > 0) {
console.log(q.length, q.shift());
q.unshift(i++);
console.log(q.length, q.shift());
q.push(i++);
console.log(q.length, q.shift());
}
```