-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
76 lines (67 loc) · 1.83 KB
/
map.js
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
"use strict";
function newObj(idx) {
return { idx:idx, name:'', count: 0 }
}
function makeId(length) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
//---------------------------------------------------------------------------------------
function main() {
const args = process.argv.slice(2);
const opType = args[0]
if (opType !== 'op1' && opType !== 'op2') {
throw new Error (`unknown operation type ${opType}`)
}
const N = 100000;
const map = new Map()
for (let i = 0; i < N; ++i) {
const obj = newObj(i)
const name = makeId(12)
obj.name = name
map.set(name, obj)
}
let cb
if (opType === 'op1' ) {
cb = () => {
for (const [k, v] of map) {
if (k !== v.name) {
v.count++
console.log(JSON.stringify(v))
}
}
}
}
else {
cb = () => {
for (const {0:k, 1:v} of map) {
if (k !== v.name) {
v.count++
console.log(JSON.stringify(v))
}
}
}
}
const runCount = 1000;
const run = () => {
for (let i = 0; i < runCount; ++i) {
cb();
}
};
run(); // warm up
globalThis.gc?.(); // gc if possible
const t0 = performance.now();
run(); // bench
const t1 = performance.now();
const time = `${(t1 - t0).toFixed(0)}`;
console.log(`Map for loop : operation = ${opType} : ${time} ms.`);
// console.log(x)
}
main()