https://github.com/lujjjh/node-cpu-count
A container-friendly alternative to os.cpus().length. Both cgroups v1 and cgroups v2 are supported.
https://github.com/lujjjh/node-cpu-count
Last synced: 19 days ago
JSON representation
A container-friendly alternative to os.cpus().length. Both cgroups v1 and cgroups v2 are supported.
- Host: GitHub
- URL: https://github.com/lujjjh/node-cpu-count
- Owner: lujjjh
- License: mit
- Created: 2022-01-15T07:23:22.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-28T00:58:53.000Z (almost 2 years ago)
- Last Synced: 2025-05-07T09:15:44.945Z (19 days ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/node-cpu-count
- Size: 124 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# node-cpu-count
A container-friendly alternative to `os.cpus().length`. Both cgroups v1 and cgroups v2 are supported.
## Installation
```sh
$ npm install node-cpu-count
```## Usage
#### `cpuCount(min = 1, max = os.cpus().length, ceiling = true): Promise`
Get available logical CPU count (CPU quota / CPU period for cgroups, fallback to `os.cpus().length`).
By default, it returns an integer by ceiling the result. The result can be used as the number of cluster workers.
`min` or `max` can be specified to determine the minimal and the maximum CPU count (which also overrides the fallback value).
#### `cpuCountSync(min = 1, max = os.cpus().length, ceiling = true): number`
A synchronous version of `cpuCount`.
## Examples
```sh
$ git clone https://github.com/lujjjh/node-cpu-count.git && cd node-cpu-count$ docker run --rm -it -v $(pwd):/app node node /app/examples
8
8$ docker run --rm -it -v $(pwd):/app --cpus=2 node node /app/examples
2
2$ docker run --rm -it -v $(pwd):/app --cpus=2.1 node node /app/examples
3
3
```