I'm using node-worker-threads-pool to process a daily function on all the documents from one collection, for that I'm using a for loop to send the id of each document to the pool, I'm trying to get the time that it takes to process the whole process but it consoles before the execution of all the id's, so I have two questions, is better to run the for loop inside of the worker file so the pool.exec() function is just called once?, and how can I log the time with the actual code?. Here is my code:
Client.find({}, {_id: 1}).then(clients => {
console.time('clientDayChange');
const pool = new StaticPool({
size: 8,
task: "./worker.js"
});
for(let client of clients){
let id = JSON.stringify(client._id);
pool.exec(id);
}
console.timeEnd('clientDayChange');
})
awaitinside anasyncfunction asthenis always such a pain.pool.execreturns a promise, so you should do something with it rather than discard the result. Also, "Is it better to..." / "what's the best way to..." is a bad way to phrase a question, because "better" and "best" are not always as objective as you think they are without clear criteria upon which to judge it. Otherwise, such questions are sometimes vague and elicit opinions. You can just try both approaches and tell us which is better for you. "How can I..." is a much better phrasing for stack overflow questions as it forces you to articulate your objectives.