Skip to content

API in Capabilities

Compute Pressure API

Published

The Compute Pressure API reports high-level pressure changes for system resources such as the CPU. A PressureObserver delivers PressureRecord objects to a callback after observe() starts watching a source.

BCD records PressureObserver from desktop Chrome 125 and mirrors that support to Edge. Chrome for Android is recorded as unsupported. Firefox and Safari are also recorded as unsupported (BCD api.PressureObserver).

const observer = new PressureObserver(callback)
observer.observe(source)
observer.observe(source, options)
observer.unobserve(source)
observer.disconnect()
observer.takeRecords()

The interface is available in secure-context windows plus dedicated and shared workers, but not service workers. observe() returns a promise that fulfills with undefined after observation begins.

The constructor receives a callback. The callback receives an array of records collected since its previous invocation or the previous takeRecords() call, plus the observer itself.

Parameter or member Type Meaning
source string The pressure source to observe. Use PressureObserver.knownSources as a hint for sources the user agent knows.
options.sampleInterval number Requested minimum interval between callbacks, in milliseconds. The default is 0.
PressureRecord.source string Source represented by the record.
PressureRecord.state string High-level pressure state.
PressureRecord.time DOMHighResTimeStamp Timestamp for the record.

unobserve(source) stops one source. disconnect() stops every source and clears queued records; takeRecords() returns queued records and empties that queue.

observe() can report policy and source failures.

Exception Condition
TypeError observe() receives a source that is not a value in the PressureSource enum.
NotAllowedError The compute-pressure Permissions Policy disallows the API.
NotSupportedError observe() receives a source the user agent does not support.

Detect PressureObserver before constructing it and retain a fixed workload mode when observation is unavailable.

This snippet writes the reported state into the document and switches a workload label for serious or critical records. The fallback keeps the reduced mode.

const status = document.body.appendChild(document.createElement("p"));
const mode = document.body.appendChild(document.createElement("p"));
if (!("PressureObserver" in globalThis)) {
status.textContent = "Compute pressure is unavailable.";
mode.textContent = "Reduced workload";
} else {
const observer = new PressureObserver((records) => {
const latest = records.at(-1);
status.textContent = `CPU pressure: ${latest.state}`;
mode.textContent = ["serious", "critical"].includes(latest.state)
? "Reduced workload"
: "Full workload";
});
observer.observe("cpu", { sampleInterval: 1000 }).catch((error) => {
status.textContent = `Pressure observation failed: ${error.name}`;
mode.textContent = "Reduced workload";
});
}

Both the missing-interface branch and a rejected observation select the reduced workload.

Checking a source and stopping observation

Section titled “Checking a source and stopping observation”

Use knownSources as a hint before observing. Still handle rejection and disconnect when the work ends.

async function watchCpu(onRecords) {
if (!("PressureObserver" in globalThis)) return null;
if (!PressureObserver.knownSources.includes("cpu")) return null;
const observer = new PressureObserver(onRecords);
try {
await observer.observe("cpu", { sampleInterval: 2000 });
return observer;
} catch {
observer.disconnect();
return null;
}
}
const observer = await watchCpu((records) => console.log(records.at(-1)?.state));
document.addEventListener("visibilitychange", () => {
if (document.hidden) observer?.disconnect();
});

A null result gives the caller one fallback signal for an absent interface or unknown source. A rejected observation produces the same signal.

Specifications

SpecificationStatus
Compute Pressure Level 1: PressureObserverW3C draft