-
Notifications
You must be signed in to change notification settings - Fork 0
/
arr.js
56 lines (49 loc) · 1.33 KB
/
arr.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
"use strict";
//---------------------------------------------------------------------------------------
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 arrays = []
let cb
if (opType === 'op1' ) {
cb = () => {
arrays.length = 0
for (let i = 0; i < N; ++i) {
arrays.push([0, 0, 0, 0, 0, 0])
}
for (let i = 0; i < N; ++i) {
arrays[i][0] = 0.2
}
}
}
else {
cb = () => {
arrays.length = 0
for (let i = 0; i < N; ++i) {
arrays.push([0.1, 0, 0, 0, 0, 0])
}
for (let i = 0; i < N; ++i) {
arrays[i][0] = 0.2
}
}
}
const runCount = 500;
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()