# Compute Pressure API

> PressureObserver reports system-resource pressure changes. This reference covers records, sampling, policy errors, cleanup, and an unsupported-browser fallback.

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.

## Where it is supported

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

## Syntax

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

## Parameters

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.

## Exceptions

[`observe()`](https://w3c.github.io/compute-pressure/#dom-pressureobserver-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. |

:::caution
The `compute-pressure` Permissions Policy has a default allowlist of `self`. When an owning document is not allowed to use that policy-controlled feature, the Compute Pressure specification requires `observe()` to return a promise rejected with `NotAllowedError` ([`PressureObserver.observe()` method steps](https://w3c.github.io/compute-pressure/#dom-pressureobserver-observe), Compute Pressure Level 1).
:::

## Examples

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

### Reducing work when CPU pressure rises

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.

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

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

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

## See also

- [Core Web Vitals](/reference/performance/core-web-vitals/) covers user-facing loading and interaction metrics
- [Generic Sensor API](/reference/capabilities/sensors/), for device sensor observations
- [Compute Pressure Level 1](https://w3c.github.io/compute-pressure/) (w3c.github.io)
- [Compute Pressure API](https://developer.mozilla.org/en-US/docs/Web/API/Compute_Pressure_API) (MDN)