Created
June 27, 2020 17:51
-
-
Save EarthenLynx/829d1e9f4113eb634b79e4336aa67733 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* The node process | |
* It's a global object that, itself as well as its methods | |
* and nested objects, can be accessed from anywhere anytime. | |
* Within the process, global variables can be defined. A prominent | |
* example is the node.env.NODE_ENV variable. It is a convention to, | |
* for instance, set the process.env.NODE_ENV to production. | |
*/ | |
console.log(process); | |
console.log(process.memoryUsage()); | |
console.log(process.eventNames()); | |
console.log(process.resourceUsage()); | |
console.log(process.uptime()); | |
console.log(process.env); | |
console.log(process.env.OS); /* Interesting to know for dependencies */ | |
console.log( | |
process.env.COMPUTERNAME | |
); /* Interesting to know to differenciate between clusters in the cloud */ | |
console.log( | |
process.env.PUBLIC | |
); /* Interesting to know for FTP clients ( file transfer protocol ) */ | |
console.log( | |
process.env.LANG | |
); /* Interesting to know for i18n or geolocation analysis */ | |
/** | |
* The process also has methods that can be called on. They are similiar to lifecycle hooks | |
* in clientside applications. | |
*/ | |
process.on("exit", () => { | |
console.log("Process has finished"); | |
}); | |
process.on("beforeExit", () => { | |
console.log("Cleaning up memory"); | |
}); | |
/* Imagine you want to check peak times of the node memory usage | |
* and log them down to an array | |
*/ | |
let counter = 1; | |
let memHistory = []; | |
setInterval(() => { | |
let uptime = process.uptime(); | |
let memUsage = process.memoryUsage(); | |
memHistory.push({ counter, memUsage }); | |
counter += 1; | |
if (uptime > 10) process.exit(); | |
}, 1000); | |
console.log("Done collecting data. "); | |
console.log("This is the memory usage history: "); | |
memHistory.forEach((el) => | |
console.log(`${el.counter}: Memory usage: ${memUsage} `) | |
); | |
/* The code above will not work properly as the results are logged before the setInterval function has finished. | |
* There are two ways to get hold if this. Promises and callback functions. | |
* The major difference between the two is the way they handle the returned object. | |
* Promises directly use callbacks on the object. That makes longer code easily maintainable. | |
* Callback functions take the returned object is a function parameter. | |
* This can possibly end in a so called callback - hell ( many chained functions at once ) | |
*/ | |
/* Example 1: Promises */ | |
const logMemUsagePromise = () => { | |
return new Promise((resolve, reject) => { | |
let counter = 0; | |
let history = []; | |
setInterval(() => { | |
let uptime = process.uptime(); | |
let memUsage = process.memoryUsage(); | |
counter += 1; | |
// The promise only resolves after 5 seconds | |
if (uptime > 5) { | |
resolve(history); | |
} | |
history.push({ counter, memUsage }); | |
}, 1000); | |
}); | |
}; | |
/* Chain the history into the Promise method */ | |
logMemUsagePromise() | |
.then((history) => { | |
history.date = new Date(); | |
console.log(history.date); | |
history.forEach((el) => | |
console.log(`${el.counter}: Heap memory usage: ${el.memUsage.heapUsed} `) | |
); | |
return history; | |
}) | |
.then((history) => console.warn(history)) | |
.then((history) => console.log("History has been chained a second time")) | |
.then(() => process.exit()); | |
/* Example 2: Callback functions. | |
* Pass a function in and call it at the end ( in the back ) of the other function | |
*/ | |
const logMemUsageCallback = (callback) => { | |
let counter = 0; | |
let history = []; | |
setInterval(() => { | |
let uptime = process.uptime(); | |
let memUsage = process.memoryUsage(); | |
counter += 1; | |
// The promise only resolves after 5 seconds | |
if (uptime > 5) { | |
if (callback) callback(history); | |
else console.log("No callback defined"); | |
} | |
history.push({ counter, memUsage }); | |
}, 1000); | |
}; | |
// /* Call the function and pass the history into the callback */ | |
logMemUsageCallback(function (history) { | |
history.forEach((el) => | |
console.log(`${el.counter} memory usage: ${el.memUsage.heapUsed}`) | |
); | |
process.exit(); | |
}); | |
/* Now while the process waits for the first function to finish, node can | |
* run other code. The same way, node can handle plenty of concurrent | |
*requests and responses from users. | |
*/ | |
setInterval(() => { | |
console.log("Still waiting for the first function ... "); | |
}, 750); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment