Get the number of processing units available
Get the number of processing units available to the current process, which may be less than the number of online processors. If this information is not accessible, then print the number of processors installed.
npm install processing-units
const processingUnits = require('processing-units');
processingUnits(); // => 4
When running a docker container with fewer cpus than the host, os.cpus().length returns the number of processors on the host.
$ docker run -it \
> --cpuset-cpus="0-1" \
> node \
> node -e "console.log(os.cpus().length)"
4
To get the number of processing units we could run nproc
$ docker run -it \
> --cpuset-cpus="0-1" \
> node \
> nproc
2
This is an abstraction.
Uses nproc on linux if awilable. When unavailable, returns the number of cpus.