-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
88 lines (70 loc) · 2.15 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
const am = require('am')
const got = require('got')
const { promisify } = require('util')
const { setImmediate } = require('timers')
const setImmediateP = promisify(setImmediate)
async function mapItem(mapFn, currentValue, index, array) {
try {
await setImmediateP()
return {
status: 'fulfilled',
value: await mapFn(currentValue, index, array)
}
} catch (reason) {
return {
status: 'rejected',
reason
}
}
}
async function worker(id, gen, mapFn, result) {
console.time(`Worker ${id}`)
for (let [ currentValue, index, array ] of gen) {
console.time(`Worker ${id} --- index ${index} item ${currentValue}`)
result[index] = await mapItem(mapFn, currentValue, index, array)
console.timeEnd(`Worker ${id} --- index ${index} item ${currentValue}`)
}
console.timeEnd(`Worker ${id}`)
}
function* arrayGenerator(array) {
for (let index = 0; index < array.length; index++) {
const currentValue = array[index]
yield [ currentValue, index, array ]
}
}
async function mapAllSettled(arr, mapFn, limit = arr.length) {
const result = []
if (arr.length === 0) {
return result
}
const gen = arrayGenerator(arr)
limit = Math.min(limit, arr.length)
const workers = new Array(limit)
for (let i = 0; i < limit; i++) {
workers.push(worker(i, gen, mapFn, result))
}
console.log(`Initialized ${limit} workers`)
await Promise.all(workers)
return result
}
const urls = []
for (let i = 0; i < 100; i++) {
urls.push(`https://www.google.com/search?q=${i}`)
}
async function mapFn(url, i) {
const contents = await got(url)
return { i, url, contents }
}
async function main() {
console.time('Promise.allSettled')
const results1 = await Promise.allSettled(urls.map(mapFn))
console.timeEnd('Promise.allSettled')
console.log('------------')
console.dir(results1)
console.time('mapAllSettled')
const results2 = await mapAllSettled(urls, mapFn, 10)
console.timeEnd('mapAllSettled')
console.log('------------')
console.dir(results2)
}
am(main)