-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfor-array.ts
155 lines (132 loc) · 3.41 KB
/
for-array.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Chance } from "chance"
const chance = new Chance()
import Benchmark from "tiny-benchy"
/* ************************************************************************* */
// parameter
import { arr_length } from "./parameters"
console.log("array size of ", arr_length)
/* ************************************************************************* */
// number array
/* ************************************************************************* */
// setup
const arr = chance.n(chance.floating, arr_length)
/* ************************************************************************* */
// functions
function for_traditional(arr: number[]) {
let sum = 0
for (let i = 0, l = arr.length; i < l; ++i) {
sum += arr[i]
}
return sum
}
function for_of(arr: number[]) {
let sum = 0
for (const a of arr) {
sum += a
}
return sum
}
function for_in(arr: number[]) {
let sum = 0
for (const i in arr) {
sum += arr[i]
}
return sum
}
function arr_reduce(arr: number[]) {
let sum = 0
sum = arr.reduce((accumulator, currentValue) => {
return accumulator + currentValue
}, sum)
return sum
}
/* ************************************************************************* */
// test
const r1 = for_traditional(arr)
const r2 = for_of(arr)
const r3 = for_in(arr)
const r4 = arr_reduce(arr)
console.assert(r1 === r2 && r2 === r3 && r3 == r4)
const suite = new Benchmark(100)
// add benchmarks
suite.add("for_traditional", () => {
for_traditional(arr)
})
suite.add("for_of", () => {
for_of(arr)
})
suite.add("for_in", () => {
for_in(arr)
})
suite.add("arr_reduce", () => {
arr_reduce(arr)
})
/* ************************************************************************* */
// string array
/* ************************************************************************* */
// setup
const arr_str = chance.n(chance.string, arr_length)
/* ************************************************************************* */
// functions
function for_traditional_str(arr_str: string[]) {
let sum = ""
for (let i = 0, l = arr_str.length; i < l; ++i) {
sum += arr_str[i]
}
return sum
}
function for_of_str(arr_str: string[]) {
let sum = ""
for (const a of arr_str) {
sum += a
}
return sum
}
function for_in_str(arr_str: string[]) {
let sum = ""
for (const i in arr_str) {
sum += arr_str[i]
}
return sum
}
function arr_reduce_str(arr_str: string[]) {
let sum = ""
sum = arr_str.reduce((accumulator, currentValue) => {
accumulator += currentValue
return accumulator
}, sum)
return sum
}
/* ************************************************************************* */
// test
const r1_str = for_traditional_str(arr_str)
const r2_str = for_of_str(arr_str)
const r3_str = for_in_str(arr_str)
const r4_str = arr_reduce_str(arr_str)
console.assert(r1_str === r2_str && r2_str === r3_str && r3_str === r4_str)
const suite2 = new Benchmark(100)
// add benchmarks
suite2.add("for_traditional_str", () => {
for_traditional_str(arr_str)
})
suite2.add("for_of_str", () => {
for_of_str(arr_str)
})
suite2.add("for_in_str", () => {
for_in_str(arr_str)
})
suite2.add("arr_reduce_str", () => {
arr_reduce_str(arr_str)
})
/* ************************************************************************* */
async function main() {
// run benchmark
console.log("\n number array \n")
await suite.run()
// run benchmark
console.log("\n string array \n")
await suite2.run()
}
main().catch((e) => {
throw e
})