-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathlist.bench.js
More file actions
120 lines (105 loc) · 3.53 KB
/
list.bench.js
File metadata and controls
120 lines (105 loc) · 3.53 KB
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
const chalk = require('chalk');
const { List } = require('../../mongodb');
const { createHistogram } = require('perf_hooks');
const { process } = require('process');
const iterations = 100;
const defaultItemsSize = 100000;
const makeBigArray = (length = defaultItemsSize) => Array.from({ length }).fill(1);
const makeReadableTime = nanoseconds => (nanoseconds / 1e6).toFixed(3).padStart(7, ' ');
const printHistogram = (name, h) => {
console.log();
console.log(chalk.green(name));
console.log('-'.repeat(155));
process.stdout.write(`| ${chalk.cyan('max')}: ${chalk.red(makeReadableTime(h.max))} ms |`);
process.stdout.write(` ${chalk.cyan('min')}: ${chalk.red(makeReadableTime(h.min))} ms |`);
process.stdout.write(` ${chalk.cyan('mean')}: ${chalk.red(makeReadableTime(h.mean))} ms |`);
process.stdout.write(` ${chalk.cyan('stddev')}: ${chalk.red(makeReadableTime(h.stddev))} ms |`);
process.stdout.write(
` ${chalk.cyan('p90th')}: ${chalk.red(makeReadableTime(h.percentile(90)))} ms |`
);
process.stdout.write(
` ${chalk.cyan('p95th')}: ${chalk.red(makeReadableTime(h.percentile(95)))} ms |`
);
process.stdout.write(
` ${chalk.cyan('p99th')}: ${chalk.red(makeReadableTime(h.percentile(99)))} ms |`
);
console.log('\n' + '-'.repeat(155));
};
const testArrayShift = () => {
let bigArray = makeBigArray();
const h = createHistogram();
for (let runs = 0; runs < iterations; runs++) {
h.recordDelta();
while (bigArray.length) bigArray.shift();
h.recordDelta();
bigArray = makeBigArray();
}
printHistogram(`shift(${defaultItemsSize}) from Array`, h);
};
const testListShift = () => {
const bigList = new List();
bigList.pushMany(makeBigArray());
const h = createHistogram();
for (let runs = 0; runs < iterations; runs++) {
h.recordDelta();
while (bigList.length) bigList.shift();
h.recordDelta();
bigList.pushMany(makeBigArray());
}
printHistogram(`shift(${defaultItemsSize}) from List`, h);
};
const testDenqueShift = () => {
const Denque = require('denque');
let bigDenque = new Denque(makeBigArray());
const h = createHistogram();
for (let runs = 0; runs < iterations; runs++) {
h.recordDelta();
while (bigDenque.length) bigDenque.shift();
h.recordDelta();
bigDenque = new Denque(makeBigArray());
}
printHistogram(`shift(${defaultItemsSize}) from Denque`, h);
};
const testArrayPush = () => {
const bigArray = [];
const h = createHistogram();
for (let runs = 0; runs < iterations; runs++) {
h.recordDelta();
for (let i = 0; i < defaultItemsSize; i++) bigArray.push(1);
h.recordDelta();
bigArray.length = 0;
}
printHistogram(`push(${defaultItemsSize}) to Array`, h);
};
const testListPush = () => {
const bigList = new List();
const h = createHistogram();
for (let runs = 0; runs < iterations; runs++) {
h.recordDelta();
for (let i = 0; i < defaultItemsSize; i++) bigList.push(1);
h.recordDelta();
bigList.clear();
}
printHistogram(`push(${defaultItemsSize}) to List`, h);
};
const testDenquePush = () => {
const Denque = require('denque');
let bigDenque = new Denque([]);
const h = createHistogram();
for (let runs = 0; runs < iterations; runs++) {
h.recordDelta();
for (let i = 0; i < defaultItemsSize; i++) bigDenque.push(1);
h.recordDelta();
bigDenque = new Denque([]);
}
printHistogram(`push(${defaultItemsSize}) to Denque`, h);
};
const main = () => {
testArrayPush();
testListPush();
testDenquePush();
testArrayShift();
testListShift();
testDenqueShift();
};
main();