-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfor-of-array.ts
64 lines (52 loc) · 1.4 KB
/
for-of-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
import { Chance } from "chance"
const chance = new Chance(12345)
const chance2 = new Chance(12345)
import Benchmark from "tiny-benchy"
/* ************************************************************************* */
// parameter
import { arr_length } from "./parameters"
console.log("array size of ", arr_length)
/* ************************************************************************* */
// number array
function arr_return() {
const arr = chance2.n(chance2.floating, arr_length)
return arr
}
/* ************************************************************************* */
// functions
function for_of() {
// array is made inside the function for fare comparison
const arr = chance.n(chance.floating, arr_length)
let sum = 0
for (const a of arr) {
sum += a
}
return sum
}
function for_of_full_lookup() {
let sum = 0
for (const a of arr_return()) {
sum += a
}
return sum
}
/* ************************************************************************* */
// test
console.assert(for_of() === for_of_full_lookup())
const suite = new Benchmark(100)
// add benchmarks
suite.add("for_of", () => {
for_of()
})
suite.add("for_of_full_lookup", () => {
for_of_full_lookup()
})
/* ************************************************************************* */
async function main() {
// run benchmark
console.log("\n number array \n")
await suite.run()
}
main().catch((e) => {
throw e
})