-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultitask.js
60 lines (41 loc) · 1.27 KB
/
multitask.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
/**
* That sample shows how the FS ReadFile - OS System pending
*/
const https = require('https')
const crypto = require('crypto')
const fs = require('fs')
const start = Date.now()
function doReq() {
https.request('https://google.com', res => {
res.on('data', _ => { })
res.on('end', _ => {
console.log(Date.now() - start)
})
}).end()
}
function doHash() {
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
console.log('Hash:', Date.now() - start)
})
}
doReq()
fs.readFile('multitask.js', 'utf8', () => {
console.log('FS:', Date.now() - start)
})
doHash()
doHash()
doHash()
doHash()
/**
* The FS Module makes use of the thread pool with crypto
*/
// That ocurred like:
// fs.readFile requires informations about Hard Driver, so, based on these workflow:
// - Node Requires some stats about hard driver before read file - PAUSE
// - HD accessed, stats returned
// - Node Require to read the file - PAUSE
// - HD accessed, file contents streamed back to app
// - Node retuns file contents
// Based on these pauses, the Thread Pool is waiting, so get the next task and process the returns of Hard Drive file
// after some thread available to next task
// BECAUSE OF THIS, doHash finish first than FS