I recently created a bot in node.js, but its super super heavy, uses 100% cpu! I use node-fetch, async.parallel and setInterval, im not sure why it uses 100% cpu, it might be maybe because setInterval queues? Idk. Here is my code though:
async.parallel({
task1: function () {
setInterval(function () {
let itemRandomizer =
config.generalInfo.itemList[
Math.floor(Math.random() * config.generalInfo.itemList.length)
];
let cookieRandomizer =
randomCookies[Math.floor(Math.random() * randomCookies.length)];
fetch(url, {
method: "GET",
agent,
headers: {
"Content-Type": "application/json",
cookie: `cookie=${cookieRandomizer}`,
},
})
.then((res) => res.json())
.then((json) => {
checks++;
//console.log(checks, itemRandomizer, json.data[0].price)
})
.catch((err) => {});
}, 0);
},
task2: function () {
/* same code as task 1 (total 20 tasks) */
},
});
I do not want to increase the interval, want it to stay as fast as it can, but 100% cpu is unreal, i dont want it to use that much, any work around? Is setInterval cpu-intensive? Threads consume more CPU than async.parallel, so thats why i dont use threads.