-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path5-each-opt.js
More file actions
44 lines (35 loc) · 799 Bytes
/
5-each-opt.js
File metadata and controls
44 lines (35 loc) · 799 Bytes
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
'use strict';
const INTERVAL = 10;
const numbers = new Array(1000).fill(1);
const each = (array, fn) => {
let time = Date.now();
let i = 0;
const last = array.length - 1;
const next = () => {
while (i <= last) {
const now = Date.now();
const diff = now - time;
if (diff > INTERVAL) {
time = now;
setTimeout(next, 0);
break;
}
fn(array[i], i++);
}
};
next();
};
let k = 0;
const timer = setInterval(() => {
console.log('next ', k++);
}, 10);
const begin = process.hrtime.bigint();
each(numbers, (item, i) => {
console.log(i);
if (i === 999) {
clearInterval(timer);
const diff = (process.hrtime.bigint() - begin) / 1000000n;
console.log('Time(ms):', diff.toString());
console.dir({ k });
}
});