get system cpu count in javascript

In Node.js, you can use the os module to get the number of CPUs available on the system. The os.cpus() method returns an array of CPU information objects, so you can simply check the length of that array to get the number of CPUs.

Here's some sample code:

index.tsx
const os = require('os');
const cpuCount = os.cpus().length;

console.log(`This system has ${cpuCount} CPUs.`);
112 chars
5 lines

The cpuCount variable will contain the number of CPUs detected by the os module. Note that this number may not correspond exactly to the number of physical CPUs in the system, as some CPUs may have multiple cores or threads.

gistlibby LogSnag