Skip to content

Instantly share code, notes, and snippets.

@CHR15-
Created January 11, 2020 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CHR15-/9c15a05826387f4f41c5f827237d4120 to your computer and use it in GitHub Desktop.
Save CHR15-/9c15a05826387f4f41c5f827237d4120 to your computer and use it in GitHub Desktop.
Node Event Loop
// node myfile.js
const pendingTimers = [];
const pendingOSTasks = [];
const pendingOperations = [];
// New timers, tasks, ops are recorded from this file running
myFile.runContents();
shouldContinue = () => {
// Check one: any pending setTimeout, setInterval, setImmediate?
// Check two: any pending OS tasks? (such as server listening to a port)
// Check three: any pending long running ops? (like fs module)
return pendingTimers.length || pendingOSTasks.length || pendingOperations.length;
}
// Entire body executes in one tick
while (shouldContinue()) {
// 1) node looks at pending timers, sees if any functions are ready to be called. (setT, setInt)
// 2) node looks at pending OS tasks and operations, executes relevant callbacks
// 3) Pause execution, continue whenever some event works...
// - a new pending OS task is done
// - a new pending operation is done
// - a timer is about to complete
// 4) Look at pending timers, call any setImmediate
// 5) Handle any 'close' events
}
// exit back to terminal
// Single threaded? While Event loop IS single threaded, some of the node framework
// executes outside the event loop.
// Example...
const crypto = require('crypto');
const start = Date.now()
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
console.log('1:', Date.now() - start + ' to run pbkdf2');
});
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
console.log('2:', Date.now() - start + ' to run pbkdf2');
});
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
console.log('3:', Date.now() - start + ' to run pbkdf2');
});
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
console.log('4:', Date.now() - start + ' to run pbkdf2');
});
crypto.pbkdf2('a', 'b', 100000, 512, 'sha512', () => {
console.log('5:', Date.now() - start + ' to run pbkdf2');
});
// 1: 956 to run pbkdf2
// 4: 959 to run pbkdf2
// 2: 962 to run pbkdf2
// 3: 982 to run pbkdf2
// 5: 1858 to run pbkdf2
// Each function takes around a second to complete, but run the script
// to find that all will finish both within around a second except the last...
// This is due to the libuv C++ module executing this code on a thread pool
// A thread pool executes outside of the event loop, taking in offloaded expensive
// computation. Tasks running in the thread pool are classified under 'pendingOperations'
// The 5th call took 2 seconds to return, This is due to the thread pool being occupied
// from the previous 4, waiting for a thread to be free to execute.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment