-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
performance.js
100 lines (77 loc) · 2.32 KB
/
performance.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
89
90
91
92
93
94
95
96
97
98
99
100
const airsync = require('../src')
const { runExample } = require('./__example-utils')
const snooze = ms => new Promise(resolve => setTimeout(resolve, ms))
const enableLog = false
const log = (...msg) => enableLog && console.log(...msg)
// takes ~50ms to return result
const getName = async (first, last = '') => {
log('getName()', first, last)
await snooze(50)
return `${first}${last ? ` ${last}` : ''}`
}
const getNameAir = airsync.convertFn(getName) // specially crafted function definition
// takes ~150ms to return result
const queryName = async (n) => {
log('queryName()', n)
await snooze(150)
return n
}
const run1 = async () => ({
king: await getName('Mansa', queryName('Musa')), // notice second param of getName() is promise
minister: await getName(queryName('Mari'), queryName('Djata')), // both params of getName() is promise
})
const run2 = async () => ({
king: await getName('Mansa'),
minister: await getName('Mari'),
})
const run3 = async () => airsync.resolve({ // using airsync
king: getName('Mansa', queryName('Musa')),
minister: getName( queryName('Mari'), queryName('Djata')),
})
// we are using getNameAir() instead of getName()
const run4 = async () => ({
king: await getNameAir('Mansa', queryName('Musa')),
minister: await getNameAir(queryName('Mari'), queryName('Djata')),
})
const run5 = async () => ({
king: await getNameAir('Mansa'),
minister: await getNameAir('Mari'),
})
const run6 = async () => airsync.resolve({ // using airsync
king: getNameAir('Mansa', queryName('Musa')),
minister: getNameAir( queryName('Mari'), queryName('Djata')),
})
runExample(run1) // 100ms
runExample(run2) // 100ms
runExample(run3) // 50ms
runExample(run4) // 200ms
runExample(run5) // 100ms
runExample(run6) // 400ms
/*
output
------
---- run3() ---
Time to finish: 50ms
{
king: 'Mansa [object Promise]',
minister: '[object Promise] [object Promise]'
}
---- run1() ---
Time to finish: 100ms
{
king: 'Mansa [object Promise]',
minister: '[object Promise] [object Promise]'
}
---- run2() ---
Time to finish: 100ms
{ king: 'Mansa', minister: 'Mari' }
---- run5() ---
Time to finish: 100ms
{ king: 'Mansa', minister: 'Mari' }
---- run6() ---
Time to finish: 200ms
{ king: 'Mansa Musa', minister: 'Mari Djata' }
---- run4() ---
Time to finish: 400ms
{ king: 'Mansa Musa', minister: 'Mari Djata' }
*/