-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path6-for-opt.js
More file actions
58 lines (52 loc) · 1.17 KB
/
6-for-opt.js
File metadata and controls
58 lines (52 loc) · 1.17 KB
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
'use strict';
const INTERVAL = 10;
const range = {
start: 1,
end: 1000,
[Symbol.asyncIterator]() {
let time = Date.now();
let value = this.start;
return {
next: () => {
const now = Date.now();
const diff = now - time;
if (diff > INTERVAL) {
time = now;
return new Promise(resolve => {
setTimeout(() => {
resolve({
value,
done: value++ === this.end + 1
});
}, 0);
});
}
return Promise.resolve({
value,
done: value++ === this.end + 1
});
}
};
}
};
console.dir({
range,
names: Object.getOwnPropertyNames(range),
symbols: Object.getOwnPropertySymbols(range),
});
let k = 0;
const timer = setInterval(() => {
console.log('next ', k++);
}, 10);
(async () => {
const begin = process.hrtime.bigint();
for await (const number of range) {
console.log(number);
if (number === range.end) {
clearInterval(timer);
}
}
const diff = (process.hrtime.bigint() - begin) / 1000000n;
console.log('Time(ms):', diff.toString());
console.dir({ k });
})();