-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.bench.ts
More file actions
48 lines (45 loc) · 1.1 KB
/
stack.bench.ts
File metadata and controls
48 lines (45 loc) · 1.1 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
import { bench, describe, expect } from 'vitest'
import c from 'chalk'
import { IStack, LinkedStack, Stack } from '../../src'
const options = {
time: 50,
iterations: 1000,
warmupTime: 100,
warmupIterations: 1000,
}
function fill(stack: IStack<number>) {
stack.push(5)
stack.push(4)
stack.push(3)
stack.push(2)
stack.push(1)
}
describe(c.blue('stack (push)'), () => {
function stackPushTest(stackType: new(options?: any) => IStack<number>) {
const stack = new stackType()
fill(stack)
}
bench('Stack', () => {
stackPushTest(Stack)
}, options)
bench('LinkedStack', () => {
stackPushTest(LinkedStack)
}, options)
})
describe(c.blue('stack (pop)'), () => {
function stackPopTest(stackType: new(options?: any) => IStack<number>) {
const stack = new stackType()
fill(stack)
expect(stack.pop()).toBe(1)
expect(stack.pop()).toBe(2)
expect(stack.pop()).toBe(3)
expect(stack.pop()).toBe(4)
expect(stack.pop()).toBe(5)
}
bench('Stack', () => {
stackPopTest(Stack)
}, options)
bench('LinkedStack', () => {
stackPopTest(LinkedStack)
}, options)
})