Typorithm · easy

node:os & System Info

node-os-process-infonodejsossystem-infocapacity
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn node:os & System Info

0%

Core Modules

node:os exposes system info: os.cpus() (cores), os.totalmem()/freemem() (RAM), os.platform(), os.hostname(), os.homedir(), and os.tmpdir(). Use it for capacity decisions like sizing a worker pool to os.cpus().length.

os.cpus() returns an array with one entry per logical core, so os.cpus().length is the standard way to count cores; os.totalmem() and os.freemem() report total and free RAM in bytes.

os.platform() returns the OS string ("darwin", "linux", "win32") for branching, os.hostname() gives the machine name, and os.homedir() and os.tmpdir() return the user's home and the OS temp directory.

The practical use is capacity planning: size a worker pool or concurrency limit to os.cpus().length, and use freemem() to back off when memory is scarce rather than hardcoding numbers per machine.

01

os.cpus().length counts logical cores.

02

os.totalmem()/freemem() report RAM in bytes.

03

os.platform(), hostname(), homedir(), tmpdir() give system paths and info.

import os from "node:os";

const cpuCount = os.cpus().length;   // number of logical cores
const total = os.totalmem();         // bytes of RAM
const free = os.freemem();           // bytes currently free
const platform = os.platform();      // "darwin" | "linux" | "win32"
const host = os.hostname();          // machine name
const home = os.homedir();           // user home directory
const tmp = os.tmpdir();             // OS temp directory

const poolSize = os.cpus().length;   // size a worker pool to the cores

Read the implementation

Connect each part of the code to the concept before you type.

1. CPU cores

os.cpus() returns one entry per logical core, so .length tells you how many cores are available.

os.cpus().length

2. Total memory

os.totalmem() reports total system RAM in bytes; os.freemem() reports what is currently free.

os.totalmem()

3. Platform

os.platform() returns the OS identifier like "darwin", "linux", or "win32" for platform branching.

os.platform()

4. Hostname

os.hostname() returns the machine's network name, handy for logging which host produced a record.

os.hostname()

5. Temp dir

os.tmpdir() returns the OS temp directory; os.homedir() returns the current user's home directory.

os.tmpdir()

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

node:os & System Info: Lesson & Practice · Typorithm | ApplyDjinn