wait-queue
A javascript wait queue object handle infinity loop tasks more efficiently
WaitQueue is an async implements of Array
Table of Contents
Examples
How to use
$ npm install wait-queue
TypeScript
;; setTimeout, 1000; run;
JS
const WaitQueue = ;const wq = ;wq; ;
Requirements
Build: Node.js >= 8.x
Properties
wq.length
Length of the WaitQueue(readonly)
wq.queue
A LinkedList, used to store the queue items, Do not modify it directly
wq.listeners
If no elements in queue yet, listener will add here, Don't modify it
Methods
wq.push(item1, [item2])
Add items to the end of the queue, will return length of the queue
wq.shift().then( item => )
Got an item from front of the queue, this is a Promise method, if there's no item in the queue, it will wait
wq.unshift(item)
Put an item in front of the queue, will return length of the queue
wq.pop().then( item => )
Got an item at the end of the queue, this is a Promise method, if there's no item in the queue, it will wait
wq.empty() alias of wq.clearQueue()
Clear the queue, Won't clear listeners
wq.clearListeners()
Clear waited listeners of the queue
Benchmark
$ git clone https://github.com/flarestart/wait-queue.git
$ cd wait-queue
$ npm install
$ npm run benchmark
Sample data in Macbook Pro MF839/8GB
1.1.0(Improve benchmark code)
Array.push(1k data) speed test x 629,538 ops/sec ±22.90% (26 runs sampled)
Array.push(1k data) 1000000 times then Array.shift() speed test x 492 ops/sec ±1.02% (89 runs sampled)
WaitQueue.push(1k data) speed test x 501,581 ops/sec ±23.45% (14 runs sampled)
WaitQueue.unshift(1k data) speed test x 447,272 ops/sec ±24.80% (14 runs sampled)
WaitQueue.shift() speed test x 315,356 ops/sec ±14.30% (52 runs sampled)
WaitQueue.pop() speed test x 240,568 ops/sec ±51.35% (39 runs sampled)
WaitQueue.push(1k data) 1000000 times then WaitQueue.shift() speed test x 476,918 ops/sec ±26.68% (29 runs sampled)
WaitQueue.push(1k data) 1000000 times then WaitQueue.pop() speed test x 471,668 ops/sec ±25.08% (31 runs sampled)
Array.push(4k data) speed test x 119,981 ops/sec ±61.52% (14 runs sampled)
Array.push(4k data) 1000000 times then Array.shift() speed test x 479 ops/sec ±1.41% (86 runs sampled)
WaitQueue.push(4k data) speed test x 147,153 ops/sec ±46.03% (14 runs sampled)
WaitQueue.unshift(4k data) speed test x 157,694 ops/sec ±37.29% (16 runs sampled)
WaitQueue.push(4k data) 1000000 times then WaitQueue.shift() speed test x 447,783 ops/sec ±28.01% (31 runs sampled)
WaitQueue.push(4k data) 1000000 times then WaitQueue.pop() speed test x 409,826 ops/sec ±30.73% (28 runs sampled)
1.0.3(Use LinkedList)
.push() 1k data speed test x 511,367 ops/sec ±31.07% (27 runs sampled)
.unshift() 1k data speed test x 269,995 ops/sec ±39.60% (14 runs sampled)
.push() 4k data speed test x 41,531 ops/sec ±12.20% (7 runs sampled)
.unshift() 4k data speed test x 35,928 ops/sec ±5.11% (8 runs sampled)
.shift() 69614.33093950555 /s
1.0.2(Use Array)
.push() 1k data speed test x 554,552 ops/sec ±26.09% (25 runs sampled)
.unshift() 1k data speed test x 132 ops/sec ±2.96% (72 runs sampled)
.push() 4k data speed test x 75,107 ops/sec ±22.32% (9 runs sampled)
.unshift() 4k data speed test x 115 ops/sec ±2.15% (71 runs sampled)
.shift() `wait too long, I didn't wait for the result, I guess is about 110/s`
Example: Iterator
use for ... of to get all values
'use strict';const WaitQueue = ;const wq = ; wq; console;for var n of wq console;
Example: Multi Worker
'use strict';const WaitQueue = ;const wq = ; // worker loop { var { // get item at the front of the queue wq; }; ;} // worker-a use 1s every task;// worker-b use 2s every task;// worker-c use 5s every task; // add a task every 500msfor var n = 0; n < 20; n++ wq;
Example: Push a function
'use strict';const WaitQueue = ;const wq = ; // there's no task herewq; // add a function as itemwq;
Example: Loop Tasks
'use strict';const WaitQueue = ;const wq = ; { // put first element out of queue wq ;}; var taskID = 0;var interval;// add a task every 1sinterval = ;
Using with co
'use strict';const WaitQueue = ;const co = ;const wq = ; ; { return { // a task will run 2s ; };} // add a task every 1svar taskID = 0;;
License
MIT