-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtip.bench.ts
More file actions
116 lines (98 loc) · 3.4 KB
/
tip.bench.ts
File metadata and controls
116 lines (98 loc) · 3.4 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
/* eslint-disable @typescript-eslint/no-unused-vars */
import {bench, describe} from 'vitest'
const SIZES = {
TINY: 10,
SMALL: 100,
MEDIUM: 1000,
LARGE: 10000,
}
describe('Function creation benchmark with different sizes', () => {
Object.entries(SIZES).forEach(([size, length]) => {
const numbers = Array.from({length}, (_, i) => i)
const double = (n: number) => n * 2
describe(`Array size: ${size} (${length})`, () => {
bench('inline function creation', () => {
numbers.map((n) => n * 2)
})
bench('reused function', () => {
numbers.map(double)
})
})
})
})
describe('Array vs Set operations with different sizes', () => {
Object.entries(SIZES).forEach(([size, length]) => {
const array = Array.from({length}, (_, i) => i)
const set = new Set(array)
const searchValue = Math.floor(length * 0.9)
describe(`Size: ${size} (${length})`, () => {
bench('Array includes', () => {
array.includes(searchValue)
})
bench('Set has', () => {
set.has(searchValue)
})
const duplicatedArray = [...array, ...array.slice(0, length * 0.1)]
bench('Array unique with filter', () => {
duplicatedArray.filter((item, index, self) => self.indexOf(item) === index)
})
bench('Array unique with Set', () => {
Array.from(new Set(duplicatedArray))
})
})
})
})
describe('Loop performance with different sizes', () => {
Object.entries(SIZES).forEach(([size, length]) => {
const items = Array.from({length}, (_, i) => i)
describe(`Size: ${size} (${length})`, () => {
bench('for...of loop', () => {
let sum = 0
for (const item of items) {
sum += item
}
})
bench('for loop with cached length', () => {
let sum = 0
const len = items.length
for (let i = 0; i < len; i++) {
sum += items[i]
}
})
bench('forEach', () => {
let sum = 0
items.forEach((item) => {
sum += item
})
})
bench('reduce', () => {
items.reduce((acc, cur) => acc + cur, 0)
})
})
})
})
describe('Map vs Object with different sizes', () => {
Object.entries(SIZES).forEach(([size, length]) => {
const keys = Array.from({length}, (_, i) => `key${i}`)
describe(`Size: ${size} (${length})`, () => {
bench('Object operations', () => {
const obj: Record<string, number> = {}
for (let i = 0; i < length; i++) {
const key = keys[i]
obj[key] = i
const value = obj[key]
delete obj[key]
}
})
bench('Map operations', () => {
const map = new Map<string, number>()
for (let i = 0; i < length; i++) {
const key = keys[i]
map.set(key, i)
const value = map.get(key)
map.delete(key)
}
})
})
})
})