-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfor-object.ts
121 lines (104 loc) · 2.56 KB
/
for-object.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
import { Chance } from "chance"
const chance = new Chance()
import Benchmark from "tiny-benchy"
/* ************************************************************************* */
// parameter
import { arr_length } from "./parameters"
console.log("object size of ", arr_length)
/* ************************************************************************* */
// obj string string
/* ************************************************************************* */
// setup
type Obj = { [p: string]: string }
const obj: Obj = {}
for (let i = 0; i < arr_length; i++) {
obj[chance.string()] = chance.string()
}
/* ************************************************************************* */
// functions
function for_traditional_keys(obj: Obj) {
let sum = ""
const keys = Object.keys(obj)
for (let i = 0, l = keys.length; i < l; ++i) {
sum += obj[keys[i]]
}
return sum
}
function for_traditional_values(obj: Obj) {
let sum = ""
const values = Object.values(obj)
for (let i = 0, l = values.length; i < l; ++i) {
sum += values[i]
}
return sum
}
function for_of_keys(obj: Obj) {
let sum = ""
const keys = Object.keys(obj)
for (const k of keys) {
sum += obj[k]
}
return sum
}
function for_of_entries(obj: Obj) {
let sum = ""
const entries = Object.entries(obj)
for (const [key, value] of entries) {
sum += value
}
return sum
}
function for_of_values(obj: Obj) {
let sum = ""
const values = Object.values(obj)
for (const value of values) {
sum += value
}
return sum
}
function for_in(obj: Obj) {
let sum = ""
for (const k in obj) {
sum += obj[k]
}
return sum
}
/* ************************************************************************* */
// test
const testout = for_traditional_keys(obj)
console.assert(
testout === for_traditional_values(obj) &&
testout === for_of_keys(obj) &&
testout === for_of_entries(obj) &&
testout === for_of_values(obj) &&
testout === for_in(obj)
)
const suite = new Benchmark(100)
// add benchmarks
suite.add("for_traditional_keys", () => {
for_traditional_keys(obj)
})
suite.add("for_traditional_values", () => {
for_traditional_values(obj)
})
suite.add("for_of_keys", () => {
for_of_keys(obj)
})
suite.add("for_of_entries", () => {
for_of_entries(obj)
})
suite.add("for_of_values", () => {
for_of_values(obj)
})
suite.add("for_in", () => {
for_in(obj)
})
/* ************************************************************************* */
async function main() {
// run benchmark
console.log("\n obj string string \n")
await suite.run()
}
main().catch((e) => {
throw e
})