# node-cpu-count

> A container-friendly alternative to os.cpus().length. Both cgroups v1 and cgroups v2 are supported.

Latest version **0.1.1** (published 2023-06-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-cpu-count
pnpm add node-cpu-count
yarn add node-cpu-count
bun add node-cpu-count
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2023-06-28 |
| First published | 2022-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 35.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Jiahao Lu |
| Maintainers | lujjjh |
| Keywords | cgroup, container, cpu, docker |

## Links

- npm: https://www.npmjs.com/package/node-cpu-count
- Repository: https://github.com/lujjjh/node-cpu-count
- Homepage: https://github.com/lujjjh/node-cpu-count#readme
- Issues: https://github.com/lujjjh/node-cpu-count/issues
- Funding: https://ko-fi.com/lujjjh
- npm.io page: https://npm.io/package/node-cpu-count

## Recent versions

- 0.1.1 (latest) — 2023-06-28
- 0.1.0 — 2022-01-15

## 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<number>`

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
```

---
_Source: https://npm.io/package/node-cpu-count · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
