Skip to content

Commit 0536a2c

Browse files
author
Flare
committed
* Add benchmark
* Improve performance(Use LinkList)
1 parent b12ba43 commit 0536a2c

5 files changed

Lines changed: 170 additions & 5 deletions

File tree

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,37 @@ $ npm install wait-queue
1515

1616
[ES5 version](https://github.com/flarestart/wait-queue-es5)
1717

18+
## Benchmark
19+
20+
```
21+
$ npm install
22+
$ npm run benchmark
23+
```
24+
25+
Sample data in Macbook Pro MF839/8GB
26+
27+
### 1.0.3(Use LinkList)
28+
29+
.push() 1k data speed test x 511,367 ops/sec ±31.07% (27 runs sampled)
30+
.unshift() 1k data speed test x 269,995 ops/sec ±39.60% (14 runs sampled)
31+
.push() 4k data speed test x 41,531 ops/sec ±12.20% (7 runs sampled)
32+
.unshift() 4k data speed test x 35,928 ops/sec ±5.11% (8 runs sampled)
33+
.shift() 69614.33093950555 /s
34+
35+
### 1.0.2(Use Array)
36+
37+
.push() 1k data speed test x 554,552 ops/sec ±26.09% (25 runs sampled)
38+
.unshift() 1k data speed test x 132 ops/sec ±2.96% (72 runs sampled)
39+
.push() 4k data speed test x 75,107 ops/sec ±22.32% (9 runs sampled)
40+
.unshift() 4k data speed test x 115 ops/sec ±2.15% (71 runs sampled)
41+
.shift() `wait too long, I didn't wait for the result, I guess is about 110/s`
42+
43+
## Change log
44+
45+
### 1.0.3
46+
47+
* Replace `wq.queue` from `Array` to `LinkList`, because do shift() operation on `Array` of 10,000,000 items cost too many time
48+
1849
## Simple Example
1950

2051
```js

benchmark/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var Benchmark = require('benchmark');
2+
var suite = new Benchmark.Suite;
3+
var WaitQueue = require('../index')
4+
var wq = new WaitQueue
5+
6+
var TimeStart = Date.now()
7+
var End = new Error('end')
8+
var Count = 0
9+
10+
// add a default consumer
11+
function loop(){
12+
wq.shift()
13+
.then(function(item){
14+
// end loop, output shift speed
15+
if(item === End){
16+
console.log('.shift()', Count * 1000 / (Date.now() - TimeStart), '/s');
17+
}
18+
setImmediate(loop)
19+
})
20+
.catch(function(e){
21+
console.log(e)
22+
})
23+
}
24+
// start loop
25+
loop()
26+
27+
// test push speed
28+
suite.add('.push() 1k data speed test', function () {
29+
Count++
30+
wq.push(Buffer.allocUnsafe(1024))
31+
})
32+
suite.add('.unshift() 1k data speed test', function () {
33+
Count++
34+
wq.unshift(Buffer.allocUnsafe(1024))
35+
})
36+
suite.add('.push() 4k data speed test', function () {
37+
Count++
38+
wq.push(Buffer.allocUnsafe(4096))
39+
})
40+
suite.add('.unshift() 4k data speed test', function () {
41+
Count++
42+
wq.unshift(Buffer.allocUnsafe(4096))
43+
})
44+
45+
suite.on('cycle', function(event) {
46+
console.log(String(event.target));
47+
})
48+
.on('error', function(err){
49+
console.log(err)
50+
})
51+
.on('complete', function() {
52+
wq.push(End)
53+
})
54+
.run({ async: true })

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
'use strict'
66
const EventEmitter = require('events').EventEmitter
7+
const LinkList = require('./libs/LinkList')
78

89
class TerminateError extends Error{
910
constructor(message){
@@ -15,7 +16,7 @@ class TerminateError extends Error{
1516
class WaitQueue extends EventEmitter{
1617
constructor(){
1718
super()
18-
this.queue = []
19+
this.queue = new LinkList
1920
this.listeners = []
2021
this.terminated = false
2122
}
@@ -35,7 +36,7 @@ class WaitQueue extends EventEmitter{
3536
}
3637
}
3738
empty(){
38-
this.queue = []
39+
this.queue = new LinkList
3940
}
4041
unshift(item){
4142
let success = false
@@ -91,7 +92,7 @@ class WaitQueue extends EventEmitter{
9192
}
9293
terminate(){
9394
this.terminated = true
94-
this.queue = []
95+
this.queue = new LinkList
9596
this._flush()
9697
this.emit('terminate')
9798
}

libs/LinkList.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Javascript WaitQueue Object in ES5
3+
* https://github.com/flarestart/wait-queue-es5
4+
*/
5+
'use strict';
6+
7+
function LinkList() {
8+
this.length = 0;
9+
this._front = null;
10+
this._end = null;
11+
}
12+
LinkList.prototype.push = function (item) {
13+
var node = {
14+
item: item,
15+
_next: null,
16+
_prev: null
17+
};
18+
if (this.length <= 0) {
19+
this._front = node;
20+
this._end = node;
21+
} else {
22+
this._end._next = node;
23+
node._prev = this._end;
24+
this._end = node;
25+
}
26+
this.length++;
27+
};
28+
LinkList.prototype.shift = function () {
29+
var item = this._front;
30+
if (item === null) {
31+
return null;
32+
}
33+
if (this._front._next != null) {
34+
this._front = this._front._next;
35+
this._front._prev = null;
36+
} else {
37+
this._front = null;
38+
this._end = null;
39+
}
40+
item._next = undefined;
41+
this.length--;
42+
return item.item;
43+
};
44+
LinkList.prototype.unshift = function (item) {
45+
var node = {
46+
item: item,
47+
_next: null,
48+
_prev: null
49+
};
50+
if (this.length <= 0) {
51+
this._front = node;
52+
this._end = node;
53+
} else {
54+
node._next = this._front;
55+
this._front._prev = node;
56+
this._front = node;
57+
}
58+
this.length++;
59+
};
60+
LinkList.prototype.pop = function () {
61+
var item = this._end;
62+
if (item === null) {
63+
return null;
64+
}
65+
if (this._end._prev != null) {
66+
this._end = this._end._prev;
67+
this._end._next = null;
68+
} else {
69+
this._front = null;
70+
this._end = null;
71+
}
72+
this.length--;
73+
item._prev = undefined;
74+
return item.item;
75+
};
76+
77+
module.exports = LinkList;

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "wait-queue",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A javascript wait queue object handle infinity loop tasks more efficiently (using ES6 class and promise)",
55
"main": "index.js",
66
"directories": {
77
"test": "tests"
88
},
99
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1"
10+
"test": "echo \"Error: no test specified\" && exit 1",
11+
"benchmark": "node ./benchmark/"
1112
},
1213
"repository": {
1314
"type": "git",
@@ -25,6 +26,7 @@
2526
},
2627
"homepage": "https://github.com/flarestart/wait-queue#readme",
2728
"devDependencies": {
29+
"benchmark": "^2.1.3",
2830
"co": "^4.6.0"
2931
}
3032
}

0 commit comments

Comments
 (0)