DEV Community

Alex Spinov
Alex Spinov

Posted on

WebNN Has a Free API You've Never Heard Of

WebNN (Web Neural Network API) is a new browser API that gives JavaScript direct access to hardware-accelerated machine learning. It runs on NPUs, GPUs, and CPUs — and it's already available in Chrome 124+.

What Makes WebNN Different?

  • Hardware acceleration — uses NPU/GPU/CPU automatically
  • Browser-native — no WASM, no ONNX runtime, just a web API
  • Privacy-first — all inference runs locally, no server needed
  • Standardized — W3C specification, supported by Intel, Google, Microsoft

The Hidden API: Neural Network Builder

WebNN provides a graph-based API for building and running ML models:

const context = await navigator.ml.createContext({deviceType: 'gpu'});
const builder = new MLGraphBuilder(context);

// Build a simple neural network
const input = builder.input('input', {dataType: 'float32', shape: [1, 784]});

// Hidden layer
const weights1 = builder.constant(
  {dataType: 'float32', shape: [784, 128]},
  new Float32Array(784 * 128)  // pretrained weights
);
const bias1 = builder.constant(
  {dataType: 'float32', shape: [128]},
  new Float32Array(128)
);
const hidden = builder.relu(builder.add(builder.matmul(input, weights1), bias1));

// Output layer
const weights2 = builder.constant(
  {dataType: 'float32', shape: [128, 10]},
  new Float32Array(128 * 10)
);
const output = builder.softmax(builder.matmul(hidden, weights2));

// Compile and run
const graph = await builder.build({output});
const results = await context.compute(graph, {
  input: new Float32Array(784).fill(0.5)
}, {output: new Float32Array(10)});

console.log('Predictions:', results.output);
Enter fullscreen mode Exit fullscreen mode

Operator API

WebNN supports a rich set of ML operators:

// Convolution for image processing
const conv = builder.conv2d(input, filter, {
  padding: [1, 1, 1, 1],
  strides: [1, 1]
});

// Batch normalization
const normalized = builder.batchNormalization(conv, mean, variance, {
  scale: gamma,
  bias: beta
});

// Pooling
const pooled = builder.averagePool2d(normalized, {
  windowDimensions: [2, 2],
  strides: [2, 2]
});
Enter fullscreen mode Exit fullscreen mode

Quick Start

// Check if WebNN is available
if ('ml' in navigator) {
  const context = await navigator.ml.createContext();
  console.log('WebNN available!');
} else {
  console.log('WebNN not supported in this browser');
}
Enter fullscreen mode Exit fullscreen mode

Why ML Engineers Are Excited

An ML engineer shared: "We moved digit recognition from our server to WebNN. Inference went from 50ms (server round-trip) to 3ms (local NPU). Our cloud bill dropped 70% and users get instant results."


Need ML-powered data tools? Email spinov001@gmail.com or check my automation solutions.

Have you experimented with WebNN? What models are you running in the browser?

Top comments (0)