Estimate available parallelism
Choose a sensible default number of workers or concurrent tasks based on the runtime environment.
Node.js · Module
The node:os module provides synchronous utilities and constants for inspecting the operating system around a Node.js process. It exposes host identity, CPU and memory information, directories, network interfaces, load, uptime, and platform conventions.
Import the built-in module with the node:os specifier, then call its synchronous functions to inspect the host platform, CPU architecture, available parallelism, memory, directories, network interfaces, and other operating-system characteristics. Most values describe the environment where the Node.js process runs, so applications should avoid assuming one platform-specific result.
import os from 'node:os';
const os = require('node:os');The module exposes operating-system utility functions and constants; individual APIs return strings, numbers, arrays, objects, or platform-specific constants as documented.
node:os specifier.require.availableParallelism() supplies a recommended parallelism estimate and always returns a positive value.loadavg() returns Unix-style one-, five-, and fifteen-minute load averages.cpus() provides per-logical-core details such as model, speed, and CPU-time counters.freemem() measures currently available system memory in bytes.tmpdir() discovers the platform’s default temporary directory.import os from 'node:os';
console.log({
platform: os.platform(),
architecture: os.arch(),
cpus: os.availableParallelism(),
memoryGiB: (os.totalmem() / 2 ** 30).toFixed(2),
tempDirectory: os.tmpdir()
});Prints portable host details using synchronous operating-system helpers.
Choose a sensible default number of workers or concurrent tasks based on the runtime environment.
Report runtime diagnostics such as platform, architecture, hostname, memory, and uptime.
Create portable file-handling code that uses the operating system’s temporary or home directory.
Inspect interfaces and addresses when exposing service diagnostics or selecting network behavior.
node:os?The module is a built-in collection of operating-system utility methods and properties. It is imported with node:os, using either ESM or CommonJS syntax, and its APIs are generally synchronous.
Follow-up: How would you choose a worker count? Use availableParallelism() rather than assuming cpus().length is the correct default, because it estimates the parallelism available to the program.
os.platform() and os.arch() return?platform() identifies the platform for which the Node.js binary was compiled, while arch() identifies its CPU architecture. They are useful for selecting platform-specific behavior, but should not be confused with arbitrary runtime machine metadata.
Follow-up: How would you distinguish a compiled target from the host kernel? Compare these APIs with the application’s deployment assumptions; the documentation defines both values in terms of the Node.js binary.
node:os APIs are useful for resource and directory discovery?totalmem() and freemem() return memory quantities in bytes. homedir(), tmpdir(), and hostname() return environment-dependent strings, so code should treat them as runtime values rather than fixed paths or names.
Follow-up: What should be done before placing sensitive files in tmpdir()? Apply application-level permissions and cleanup policies; the module only identifies the operating system’s temporary-file directory.
availableParallelism()arch()cpus()endianness()freemem()homedir()hostname()loadavg()