WebGPU: The GPU Comes to JavaScript
Your computer has two processors: a CPU and a GPU. For decades, web developers could only use the CPU. Now, with WebGPU, JavaScript can tap into the massive parallel processing power of the graphics card.
This isn't just about prettier graphics. It's about running machine learning models, processing massive datasets, and building experiences that weren't possible before.
What is a GPU?
A CPU (Central Processing Unit) is designed for complex, sequential tasks. It has a few powerful cores that excel at doing one thing at a time, very fast.
A GPU (Graphics Processing Unit) is the opposite. It has thousands of smaller cores designed to do many simple tasks simultaneously. This is called parallel processing.
Originally, GPUs were built for rendering graphicsβcalculating millions of pixel colors simultaneously. But researchers realized this parallel architecture is perfect for many other tasks: matrix math, physics simulations, and especially machine learning.
WebGPU vs WebGL
Before WebGPU, we had WebGL (and WebGL 2). WebGL is based on OpenGL ES, a graphics API from 2007. It works, but it has limitations:
| Feature | WebGL | WebGPU |
|---|---|---|
| Based on | OpenGL ES (2007) | Vulkan/Metal/D3D12 (modern) |
| Compute shaders | No | Yes |
| Primary use | Graphics only | Graphics + General compute |
| API style | State machine (error-prone) | Command buffers (explicit) |
| Error handling | Silent failures | Proper error messages |
| ML inference | Hacky workarounds | Native support |
The key innovation is compute shaders. WebGL could only run code as part of rendering graphics. WebGPU can run arbitrary parallel computations without rendering anythingβperfect for data processing and machine learning.
Getting Started with WebGPU
Here's how to check if WebGPU is available and get a reference to the GPU:
The adapter represents the physical GPU. The device
is your interface to itβyou'll use it to create buffers, write shaders, and
dispatch work to the GPU.
A Simple Compute Example
Let's multiply every number in an array by 2βon the GPU. This is overkill for small arrays, but the pattern scales to millions of elements.
The shader code is written in WGSL (WebGPU Shading Language).
It looks a bit like Rust. The @compute decorator marks this as a
compute shader, and @workgroup_size(64) means 64 GPU threads will
run in parallel.
Real-World Use Cases
Machine Learning
Neural networks are essentially giant matrix multiplicationsβexactly what GPUs excel at. Libraries like Transformers.js use WebGPU to run AI models directly in your browser. The chat assistant on this site uses this approachβcheck out the browser AI post to see how it works.
Data Visualization
Rendering millions of data points in real-time is a perfect GPU task. Libraries like deck.gl use WebGPU to render massive datasetsβthink city-scale maps with millions of points.
3D Graphics and Games
While WebGL handles 3D graphics fine, WebGPU enables more advanced techniques: deferred rendering, compute-based particle systems, and better performance overall. Game engines like Babylon.js now support WebGPU.
Scientific Computing
Physics simulations, fluid dynamics, genetic algorithmsβany computation that can be parallelized benefits from GPU acceleration.
Browser Support
WebGPU became widely available in late 2024:
- Chrome 113+ β Full support
- Edge 113+ β Full support (Chromium-based)
- Safari 18+ β Full support (macOS/iOS)
- Firefox β Behind a flag (dom.webgpu.enabled)
For users without WebGPU, you can fall back to WebGL (for graphics) or WebAssembly (for compute). Most ML libraries like Transformers.js handle this automatically.
WebGPU vs Native
How does WebGPU compare to native GPU APIs like CUDA or Metal?
- Performance: WebGPU is typically 80-95% of native speed. The browser adds some overhead, but it's surprisingly close.
- Portability: WebGPU works everywhere with a modern browser. CUDA only works on NVIDIA GPUs; Metal only works on Apple devices.
- Ease of use: No installation, no drivers, no compilation. Just JavaScript.
For production ML workloads, you'll still want server-side inference. But for privacy-sensitive applications, demos, education, and offline useβWebGPU is genuinely capable.
Try It Yourself
The easiest way to experiment with WebGPU is through higher-level libraries:
- Transformers.js β Run ML models in the browser
- Babylon.js β 3D graphics engine with WebGPU support
- Three.js β Popular 3D library (WebGPU renderer in progress)
- WebGPU Fundamentals β Low-level tutorials
The Bigger Picture
WebGPU represents a broader trend: the browser becoming a serious application platform. Between WebGPU for compute, WebAssembly for near-native code, and modern JavaScript APIsβthe web can now do things that once required native apps.
The chat assistant running on this site? That's a language model doing inference entirely in your browser, accelerated by your GPU, with zero server calls. Five years ago, that was science fiction.
Further Reading
- WebGPU API β MDN Web Docs
- WebGPU Specification β W3C
- WGSL Specification β WebGPU Shading Language
- WebGPU Fundamentals β Tutorial site
- AI in the Browser β WebGPU powering ML inference