When an app processes hundreds of thousands of values per step, such as a physics simulation, a matrix multiplication, or a Monte Carlo model, a JavaScript loop blocks rendering, and even Web Workers are limited by the number of CPU cores. WebGPU compute shaders run the same calculation on thousands of GPU threads in parallel.
Choose where to run the computation
- WebGPU compute shaders (this guide): calculations that do a lot of arithmetic per value over large datasets, such as matrix multiplication, N-body or particle simulations, Monte Carlo sampling, or image-sized grids. Results that stay on the GPU between steps benefit the most.
- A Web Worker, optionally with WebAssembly SIMD: light per-element work (for example, scaling or normalizing an array), small datasets, or calculations that need the result after every step. Copying data to the GPU and reading it back has a fixed cost that can exceed the time the calculation takes on the CPU.
To decide, measure both paths with realistic data sizes. Keep data on the GPU across iterations, and read back only the values you need.
Create the GPU device
Request a new adapter each time you create a device, including after device loss: an adapter can create only one device.
requestAdapter()resolves tonullwhen no suitable GPU is available.requestDevice()can reject, for example when the requested features or limits aren't supported. Treat both cases as "use the fallback".- Request only the optional features and limits your app uses. A device enforces exactly the limits it was created with, so an app that requests only what it needs gets the same validation errors on every device, instead of depending on the limits of the development machine. Higher limits can also cost performance on some devices.
- Request
shader-f16only when the app usesf16and the adapter supports it. Requesting an unsupported feature makesrequestDevice()reject. - The default
maxStorageBufferBindingSize(128 MiB) andmaxBufferSize(256 MiB) are enough for most apps. Request larger limits only when your data needs them, and no more than the adapter supports.
const LARGE_BUFFER_BYTES = 1024 * 1024 * 1024; // 1 GiB: set this to the largest buffer your app creates
// Returns null when the GPU path isn't available; use the fallback then.
async function createGpuDevice({ f16 = false, largeBuffers = false } = {}) {
if (!('gpu' in navigator)) return null;
try {
const adapter = await navigator.gpu.requestAdapter(); // New adapter per device
if (!adapter) return null;
// Only request what's needed. The device enforces exactly the features and limits it
// was created with, so exceeding a default limit fails on every device, not only on
// users' devices with lower limits than the development machine.
const supportsF16 = f16 && adapter.features.has('shader-f16');
const requiredLimits = {};
if (largeBuffers) {
requiredLimits.maxBufferSize = Math.min(LARGE_BUFFER_BYTES, adapter.limits.maxBufferSize);
requiredLimits.maxStorageBufferBindingSize =
Math.min(LARGE_BUFFER_BYTES, adapter.limits.maxStorageBufferBindingSize);
}
const device = await adapter.requestDevice({
requiredFeatures: supportsF16 ? ['shader-f16'] : [],
requiredLimits,
});
return { device, supportsF16 };
} catch {
return null;
}
}
If you need more resources, for example to process datasets as large as the GPU allows, request the adapter's maximums instead. Check the resulting device.limits before allocating, because the maximums differ between devices:
const device = await adapter.requestDevice({
requiredLimits: {
maxBufferSize: adapter.limits.maxBufferSize,
maxStorageBufferBindingSize: adapter.limits.maxStorageBufferBindingSize,
},
});
This guide uses f32 data, so it doesn't request f16. Request larger buffer limits only when the data doesn't fit in the default 128 MiB storage buffer binding:
const DEFAULT_MAX_BINDING_BYTES = 128 * 1024 * 1024;
const gpu = await createGpuDevice({ largeBuffers: dataByteLength > DEFAULT_MAX_BINDING_BYTES });
Write the compute shader
Each invocation computes one output value. Pass the element count in a uniform buffer, and skip invocations past the end, because the last workgroup is usually partial. This example advances a damped spring by a number of time steps for each element:
const WORKGROUP_SIZE = 64; // Use one constant for the shader and the dispatch
const module = device.createShaderModule({
code: `
struct Params { count: u32 };
@group(0) @binding(0) var<uniform> params: Params;
@group(0) @binding(1) var<storage, read> inputData: array<f32>;
@group(0) @binding(2) var<storage, read_write> outputData: array<f32>;
@compute @workgroup_size(${WORKGROUP_SIZE})
fn main(@builtin(global_invocation_id) id: vec3u, @builtin(num_workgroups) groups: vec3u) {
let i = id.x + id.y * groups.x * ${WORKGROUP_SIZE}u; // 2D dispatch, see below
if (i >= params.count) { return; } // Skip the partial last workgroup
var x = inputData[i]; // Example: initial spring displacement
var v = 0.0;
for (var n = 0; n < 100; n++) {
v += (-4.0 * x - 0.5 * v) * 0.01;
x += v * 0.01;
}
outputData[i] = x;
}
`,
});
const pipeline = await device.createComputePipelineAsync({
layout: 'auto',
compute: { module, entryPoint: 'main' },
});
Choose the workgroup size:
- 64 for 1D data: a good starting point. It's a multiple of the 32- or 64-thread execution width of most GPUs, and within the default limits.
- 8×8 or 16×16 for 2D grids, such as images or matrices:
@workgroup_size(8, 8). - Tune with measurements. Memory-bound kernels often run well at 128 or 256; kernels that use
var<workgroup>shared memory may need smaller workgroups.
Workgroup sizes up to 256 invocations in total (and up to 64 in the z dimension) work on every device without checks, because they're within the default limits. Larger sizes require requesting higher maxComputeInvocationsPerWorkgroup and maxComputeWorkgroupSize* limits, which not every adapter supports.
A single dispatch dimension allows at most 65,535 workgroups (maxComputeWorkgroupsPerDimension), which covers about 4.19 million elements at a workgroup size of 64. A larger 1D dispatch is a validation error. To support any element count, always dispatch in two dimensions, and compute the index from both, as the shader above does:
// Workgroup counts for a 2D dispatch that covers `count` elements.
function workgroupCounts(count) {
const groups = Math.ceil(count / WORKGROUP_SIZE);
const groupsX = Math.min(groups, device.limits.maxComputeWorkgroupsPerDimension);
return [groupsX, Math.ceil(groups / groupsX)];
}
Create buffers once
Create the buffers and the bind group once, when the GPU path starts, and reuse them for every run. A storage buffer can't be mapped for JavaScript to read, because a buffer with MAP_READ usage can have no other usage except COPY_DST. Create a separate staging buffer to copy the results into:
const bytes = count * 4; // f32 values: always a multiple of 4 bytes
const paramsBuffer = device.createBuffer({ size: 4, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });
const inputBuffer = device.createBuffer({ size: bytes, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST });
const outputBuffer = device.createBuffer({ size: bytes, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC });
const stagingBuffer = device.createBuffer({ size: bytes, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST });
device.queue.writeBuffer(paramsBuffer, 0, new Uint32Array([count]));
const bindGroup = device.createBindGroup({
layout: pipeline.getBindGroupLayout(0),
entries: [
{ binding: 0, resource: { buffer: paramsBuffer } },
{ binding: 1, resource: { buffer: inputBuffer } },
{ binding: 2, resource: { buffer: outputBuffer } },
],
});
Wrap this setup in error scopes, as shown in Handle GPU errors and device loss. The buffers and params.count are sized for one element count. When the count changes, destroy the old buffers, and create new buffers, a new bind group, and a new params.count value.
Run the computation and read results back
For each run, write the input, dispatch, copy the output to the staging buffer, and map it. unmap() detaches the mapped range, so copy or process the values before calling it:
async function runOnGpu(input) {
device.queue.writeBuffer(inputBuffer, 0, input); // Only the input changes per run
device.pushErrorScope('validation');
const encoder = device.createCommandEncoder();
const pass = encoder.beginComputePass();
pass.setPipeline(pipeline);
pass.setBindGroup(0, bindGroup);
pass.dispatchWorkgroups(...workgroupCounts(input.length)); // groupsX, groupsY
pass.end();
encoder.copyBufferToBuffer(outputBuffer, 0, stagingBuffer, 0, input.byteLength);
device.queue.submit([encoder.finish()]);
if (await device.popErrorScope()) throw new Error('GPU run failed validation');
await stagingBuffer.mapAsync(GPUMapMode.READ);
const result = new Float32Array(stagingBuffer.getMappedRange().slice(0));
stagingBuffer.unmap();
return result;
}
- Run one computation at a time per staging buffer.
mapAsync()fails while the buffer is already mapped or being mapped. For continuous readback, rotate between two or more staging buffers. - Skip readback when you don't need the values on the CPU. For example, a simulation that renders particles can bind the same storage buffer as vertex data, if you add
GPUBufferUsage.VERTEXto its usage. - Read back only what you need, such as a summary computed on the GPU, rather than the full array.
Run the computation off the main thread
Run device creation, uploads, and readback in a dedicated module Worker, so input, scrolling, and rendering stay responsive. WebGPU is available in workers. Create the GPU resources on the first request, and fall back to the CPU when the GPU path isn't available or fails:
// main.js
const worker = new Worker(new URL('./compute-worker.js', import.meta.url), { type: 'module' });
worker.addEventListener('message', ({ data }) => (data.error ? showError(data.error) : showResult(data.result)));
runButton.addEventListener('click', () => {
const input = readInputValues(); // A new Float32Array for every run
worker.postMessage({ input }, [input.buffer]); // Transfer instead of copying
});
// compute-worker.js
let gpu; // GPU resources (undefined until the first run), or null for the CPU fallback
let queue = Promise.resolve();
self.addEventListener('message', ({ data }) => {
// Run one computation at a time: the staging buffer can't be mapped twice at once.
// The catch keeps the queue running after a failed run.
queue = queue.then(() => compute(data.input)).catch((error) => {
self.postMessage({ error: error.message });
});
});
async function compute(input) {
if (gpu === undefined) {
gpu = await setUpGpu(input.length); // createGpuDevice(), pipeline, buffers, and bind group, or null
} else if (gpu && gpu.count !== input.length) {
gpu.resize(input.length); // New buffers, bind group, and params.count for the new size
}
let result = null;
if (gpu) {
try {
result = await gpu.run(input); // runOnGpu() from the previous section
} catch {
gpu.device.destroy(); // Release GPU memory, and stop using the GPU path
gpu = null;
}
}
result ??= computeOnCpu(input); // See "WebGPU fallback"
self.postMessage({ result }, [result.buffer]);
}
Handle GPU errors and device loss
GPU setup and execution can fail after detection succeeds. Most WebGPU calls don't throw: createBuffer(), createBindGroup(), encoder commands such as dispatchWorkgroups() or draw(), and submit() report validation and out-of-memory errors to error scopes, or to the device's uncapturederror event, and return objects that are silently invalid. An invalid submit is dropped, and mapAsync() can still resolve with stale or zeroed data. One exception: device.queue.writeBuffer() throws an OperationError when the number of bytes to write isn't a multiple of 4, which is why f16 data needs padding. In each case, switch to the fallback (a CPU implementation for computations, or another renderer for graphics) instead of using the result:
- Setup fails:
createGpuDevice()returnsnull,createComputePipelineAsync()orcreateRenderPipelineAsync()rejects, or an error scope around buffer and bind group creation reports an error. - Data exceeds the device limits: buffers larger than
maxBufferSize, or bindings larger thanmaxStorageBufferBindingSize, produce a validation error. Check sizes before allocating. - The GPU runs out of memory: allocation failures are reported as a
GPUOutOfMemoryErrorthrough an'out-of-memory'error scope. - A run fails: wrap each run's
encoder.finish()andsubmit()in a'validation'error scope, because invalid encoder commands are reported when the encoder finishes. Discard the result if the scope reports an error.mapAsync()rejects if the device is lost, andpopErrorScope()then resolves tonull. - An error escapes every scope: it's reported to the
uncapturederrorevent. - The device is lost (driver reset or GPU process crash):
device.lostresolves. Switch to the fallback, or create a new device with a new adapter.
// Setup: check limits, then capture allocation and validation errors.
const maxBytes = Math.min(device.limits.maxBufferSize, device.limits.maxStorageBufferBindingSize);
if (data.byteLength > maxBytes) return useFallback();
device.pushErrorScope('out-of-memory');
device.pushErrorScope('validation');
const dataBuffer = device.createBuffer({
size: data.byteLength,
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
});
const validationError = await device.popErrorScope(); // Scopes pop in reverse order
const oomError = await device.popErrorScope();
if (validationError || oomError) return useFallback();
device.queue.writeBuffer(dataBuffer, 0, data); // Write only to a buffer known to be valid
// Each run: an invalid command buffer is dropped without an exception.
device.pushErrorScope('validation');
device.queue.submit([encoder.finish()]);
if (await device.popErrorScope()) return useFallback();
// Errors outside any error scope, and device loss. One failure can trigger several of
// these (a single invalid pass reports errors from both finish() and submit()), so react
// only while this device is still the active one.
device.addEventListener('uncapturederror', () => {
if (gpu?.device === device) useFallback();
});
device.lost.then((info) => {
if (info.reason !== 'destroyed' && gpu?.device === device) useFallback();
});
In this snippet, gpu holds the active GPU resources after setup succeeds. useFallback() switches to the fallback, destroys the device, and sets gpu to null. Make it safe to call more than once, for example with a flag that records that the fallback is already active.
Verify before finishing
Test the result. Check each of these, and fix and re-test any that fail:
- No WebGPU: with WebGPU unavailable, or with
requestAdapter()resolving tonull, the calculation still produces results on the CPU. - Correctness: GPU results match the CPU fallback within floating-point tolerance, including for an element count that isn't a multiple of the workgroup size.
- Repeated runs: running the calculation several times works, and reuses the device, pipeline, and buffers.
- Responsiveness: the page keeps responding to input while the calculation runs.
Alternative solutions
Some open-source libraries run data-parallel calculations on the GPU with WebGPU, for example Babylon.js and Three.js (compute shaders inside their rendering engines), TensorFlow.js (tensor and matrix operations), and TypeGPU (typed WebGPU in TypeScript). Choose based on what your project already uses:
- If the app already renders with an engine, use that engine's compute support, so compute and rendering share one device and its buffers.
- If the calculation maps to tensor operations, such as matrix multiplication or element-wise math, a tensor library can be used.
When you use a library, apply the same practices as the native path: check for a non-null adapter, run the work in a Worker, reuse resources across runs, and keep a CPU fallback.
Fallback strategies
WebGPU fallback
When 'gpu' in navigator is false, requestAdapter() resolves to null, or GPU setup or a run fails, run the same calculation on the CPU in the Worker. The calculation stays available, more slowly for large datasets. For heavy calculations, compile the CPU path to WebAssembly with SIMD. Results are comparable to the GPU results, but not always bit-identical, because the order and precision of floating-point operations can differ.
function computeOnCpu(input) {
const output = new Float32Array(input.length);
for (let i = 0; i < input.length; i++) {
let x = input[i];
let v = 0;
for (let n = 0; n < 100; n++) {
v += (-4 * x - 0.5 * v) * 0.01;
x += v * 0.01;
}
output[i] = x;
}
return output;
}
Module workers
If your Baseline target doesn't include module workers, build the worker as a classic script, for example with your bundler's worker output format, and create it without { type: 'module' }. Inside a classic worker, load dependencies with importScripts() instead of import.
WebAssembly SIMD
If you compile the CPU fallback to WebAssembly with SIMD and your Baseline target doesn't include it, build a second module without SIMD, and load it when the browser can't validate SIMD instructions:
// A tiny module that uses one SIMD instruction: validation fails without SIMD support.
const simdTest = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 5, 1, 96, 0, 1, 123, 3, 2, 1, 0, 10, 10, 1, 8, 0, 65, 0, 253, 15, 253, 98, 11]);
const wasmUrl = WebAssembly.validate(simdTest) ? 'compute-simd.wasm' : 'compute.wasm';
The plain JavaScript loop shown in "WebGPU fallback" is also a valid fallback.