I need to clarify something about Node.js, Promises, CPU and performance.
To set up a context, I will talk about an asynchronous processing (database query) executed multiple times (in a loop) and then do something else only after all async. processings are done.
Let's start with a code example :
async function databaseQuery() {
return await connection.query('SELECT * FROM example;');
}
I want to perform n times an async call (databaseQuery
function), and when these n executions are over, do something else.
Let's use parallels Promises to achieve this goal :
const array = [...]; // Assuming this array is full of whatever
const promises = array.map(async (item) => {
return await databaseQuery();
});
await Promise.all(promise);
// Ok now I'm sure all async calls are done
I tried an implemention of this code in two environnements :
- Local Machine, Windows 10 x64, Intel i7 6c/12t , 16g RAM
- Remote server (virtualized server @ OVH), Ubuntu 16.04, 1vCore, 6g RAM
Obviously, performance on the local machine are far away better than on the remote one (< 1 second vs. > 1 minute).
But I need some precisions on Why ?. I'm aware that physical materials are way better on the local machine.
Moreover, as Node.js is running on a single thread on a single core why the Windows resources monitor the Node.js process using 10 threads and 6 processors ?
There is no "multi-processing" code implemented in the code (using cluster
for example).
If I want to put this code in production, should I pay attention of the processor number of cores, or it is a Windows internal process management ?
Please help me clarify this situation, I really want to understand how does this work in the background, as it will help to choose right configuration to run this type of code.
promises = array.map(databaseQuery);
presumably?connection.query
do something that creates some child process. What library is it?