Skip to Content
💡The WebNN Origin Trial is coming! Developers can sign up for trial keys to explore →
LearnTutorialsFramework vs Vanilla

JavaScript ML Frameworks vs Vanilla JavaScript

WebNN can be used either through supported JavaScript machine learning frameworks or directly via vanilla JavaScript. Choose the approach that best suits your needs.

JavaScript ML Frameworks

WebNN API is integrating into major JavaScript machine learning frameworks like ONNX Runtime Web, Transformers.js, and LiteRT.js. This enables web developers to build AI applications using these high-level frameworks with less code while leveraging WebNN’s efficient, low-level neural network operations.

FrameworkModel formatBest forSelect WebNN with
Transformers.jsONNX models from the Hugging Face HubTask pipelines with pre- and post-processing included, in a few linesdevice: 'webnn-gpu'
ONNX Runtime Web.onnxFull control over sessions, shapes and tensorsexecutionProviders: ['webnn']
LiteRT.js.tfliteModels from LiteRT or converted from PyTorch, and an NPU pathaccelerator: 'webnn'

Whichever framework you choose, WebNN needs fixed tensor shapes, so plan to set free dimension overrides or export models with static shapes, and keep a wasm fallback for browsers without WebNN. If something goes wrong, see Troubleshooting.

Vanilla JavaScript

Use the WebNN API directly to create custom computational graphs and neural network models with greater flexibility in your AI applications. However, building models with the raw WebNN API requires more complex code compared to using mature solutions like ONNX and TFLite.

Using the WebNN JavaScript API directly instead of a ML framework reduces application size by eliminating framework distributions overhead.

Last updated on