-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.bench.ts
80 lines (75 loc) · 2.02 KB
/
queue.bench.ts
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
import { bench, describe, expect } from 'vitest'
import c from 'chalk'
import { Dequeue, IQueue, LinkedQueue, numberComparator, PriorityQueue, Queue } from '../../src'
const options = {
time: 50,
iterations: 1000,
warmupTime: 100,
warmupIterations: 1000,
}
function fill(queue: IQueue<number>) {
queue.enqueue(5)
queue.enqueue(4)
queue.enqueue(3)
queue.enqueue(2)
queue.enqueue(1)
}
describe(c.blue('queue (enqueue)'), () => {
function queueAddTest(queueType: new(options?: any) => IQueue<number>, isPriorityQueueTested = false) {
let queue
if (isPriorityQueueTested) {
queue = new queueType(numberComparator)
} else {
queue = new queueType()
}
fill(queue)
}
bench('Queue', () => {
queueAddTest(Queue)
}, options)
bench('LinkedQueue', () => {
queueAddTest(LinkedQueue)
}, options)
bench('PriorityQueue', () => {
queueAddTest(PriorityQueue, true)
}, options)
bench('Dequeue', () => {
queueAddTest(Dequeue)
}, options)
})
describe(c.blue('queue (dequeue)'), () => {
function queueDequeueTest(queueType: new(options?: any) => IQueue<number>, isPriorityQueueTested = false) {
let queue
if (isPriorityQueueTested) {
queue = new queueType(numberComparator)
} else {
queue = new queueType()
}
fill(queue)
if (isPriorityQueueTested) {
expect(queue.dequeue()).toBe(1)
expect(queue.dequeue()).toBe(2)
expect(queue.dequeue()).toBe(3)
expect(queue.dequeue()).toBe(4)
expect(queue.dequeue()).toBe(5)
} else {
expect(queue.dequeue()).toBe(5)
expect(queue.dequeue()).toBe(4)
expect(queue.dequeue()).toBe(3)
expect(queue.dequeue()).toBe(2)
expect(queue.dequeue()).toBe(1)
}
}
bench('Queue', () => {
queueDequeueTest(Queue)
}, options)
bench('LinkedQueue', () => {
queueDequeueTest(LinkedQueue)
}, options)
bench('PriorityQueue', () => {
queueDequeueTest(PriorityQueue, true)
}, options)
bench('Dequeue', () => {
queueDequeueTest(Dequeue)
}, options)
})