Last active
September 19, 2018 07:16
-
-
Save codegino/ff82de47ca02c5d82737485a5c5c0bd3 to your computer and use it in GitHub Desktop.
Advanced NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https'); | |
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | |
const start = Date.now(); | |
function doRequest() { | |
https.request('https://www.google.com', res => { | |
res.on('data', () => {}) | |
res.on('end', () => { | |
console.log(Date.now() - start) | |
}) | |
}) | |
.end(); | |
} | |
doRequest(); | |
doRequest(); | |
doRequest(); | |
doRequest(); | |
doRequest(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node myFile.js | |
const pendingTimers = []; | |
const pendingOSTasks = []; | |
const pendingOperations = []; | |
// New timers, tasks, operations are recorded from myFile running | |
myFile.runContents(); | |
function shouldContinue() { | |
// Check one: Any pending setTimeout, setInterval, setImmediate? | |
// Check two: Any pending OS tasks? (Like server listening to port) | |
// Check three: Any pending long running operations? (Like fs module) | |
return pendingTimers.length || pendingOSTasks.length || pendingOperations.length; | |
} | |
// Entire body executes in one 'tick' | |
while(shouldContinue()) { | |
// 1) Node looks at pendingTimers and sees if any functions are ready to be called. setTimeout, setInterval | |
// 2) Node looks at pendingOSTasks and pendingOperations and calls relevant callbacks | |
// 3) Pause execution. Continue when... | |
// - a new pendingOSTask is done | |
// - a new pendingOperation is done | |
// - a timer is about to complete | |
// 4) Look at pendingTimers. Call any setImmediate | |
// 5) Handle any 'close' events | |
} | |
// exit back to terminal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require('crypto'); | |
const start = Date.now(); | |
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => { | |
console.log('1:', Date.now() - start); | |
}); | |
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => { | |
console.log('2:', Date.now() - start); | |
}); | |
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => { | |
console.log('3:', Date.now() - start); | |
}); | |
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => { | |
console.log('4:', Date.now() - start); | |
}); | |
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => { | |
console.log('5:', Date.now() - start); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment