Skip to content

Instantly share code, notes, and snippets.

@brandon93s
Created December 9, 2017 19:19
Show Gist options
  • Save brandon93s/a46fb07b0dd589dc34e987c33d775679 to your computer and use it in GitHub Desktop.
Save brandon93s/a46fb07b0dd589dc34e987c33d775679 to your computer and use it in GitHub Desktop.
// Source of https://www.npmjs.com/package/physical-cpu-count
'use strict'
const os = require('os')
const childProcess = require('child_process')
function exec (command) {
const output = childProcess.execSync(command, {encoding: 'utf8'})
return output
}
let amount
const platform = os.platform()
if (platform === 'linux') {
const output = exec('lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l')
amount = parseInt(output.trim(), 10)
} else if (platform === 'darwin') {
const output = exec('sysctl -n hw.physicalcpu_max')
amount = parseInt(output.trim(), 10)
} else if (platform === 'windows') {
const output = exec('WMIC CPU Get NumberOfCores')
amount = output.split(os.EOL)
.map(function parse (line) { return parseInt(line) })
.filter(function numbers (value) { return !isNaN(value) })
.reduce(function add (sum, number) { return sum + number }, 0)
} else {
const cores = os.cpus().filter(function (cpu, index) {
const hasHyperthreading = cpu.model.includes('Intel')
const isOdd = index % 2 === 1
return !hasHyperthreading || isOdd
})
amount = cores.length
}
module.exports = amount
@LeMoussel
Copy link

LeMoussel commented May 16, 2019

In a simpler way, with Node JS : os.cpus()

Returns an array of objects containing information about each CPU/core installed: model, speed (in MHz), and times (an object containing the number of milliseconds the CPU/core spent in: user, nice, sys, idle, and irq).

The length of this array is the number of "processors" in the system. Most systems only have one CPU, so that's the number of cores of that CPU.

@earslap
Copy link

earslap commented Dec 16, 2019

os.cpus() is not the same thing - it will return the logical cores, and the code above gives you physical cores. If you have an Intel CPU with hyperthreading enabled, and have 4 physical cores, os.cpus().length will be 8, but you actually have 4 physical cores. If you want 8 for some reason, os.cpus() will give you what you want but if you want 4 (most likely what you want) then you need the above code.

@danilobassi8
Copy link

I think this is not working for my AMD Ryzen 7 1800x. I'm still getting 16 instead of 8

@jakoboo
Copy link

jakoboo commented Oct 2, 2020

That's because os.platform() returns 'win32' for windows and not 'windows' as here. Check my fork for proper solution.

@tanov
Copy link

tanov commented Mar 19, 2021

I am getting 'win32' for windows, so it falls back to the cpus(). I changed it to else if (platform === 'windows' || platform === 'win32') {

@sageyx2002
Copy link

Node 18.14.0 and newer support os.availableParallelism() which should be used instead of os.cpus().length, this still only returns the number of logical processors, not the number of physical processors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment